jsp file download

A

anilkumar05

I am trying to write a JSP, using which user can download an xml
document..
the following is the code iam using..but in the output at the end od
document it is displaying a question mark, because of which it is
blowing up when i try to open the downloaded xml document..
please help me to remove the extra question mark at the end of
document..

<%@ page import="java.util.*,
java.io.*"
%>
<%@ page language="java"
session="false"

%>
<%
String fpath=request.getParameter("filePath");
//read the file name.
File fFile = new File (fpath);
int i=fpath.lastIndexOf("\\");
String stFileName;
if(i>0)
{
stFileName = fpath.substring(i+1);
}
else
stFileName = fpath;

response.setContentType( "text/xml" );

response.setHeader ("Content-Disposition",
"attachment;filename=\""+stFileName+"\"");

InputStream isStream = null;

BufferedInputStream in = new BufferedInputStream(new
FileInputStream(fFile));

//ServletOutputStream outs = response.getOutputStream();

int bit = 256;

try {
while ((bit) >= 0) {
bit = in.read();
out.write(bit);
}
} catch (IOException ioe) {
ioe.printStackTrace(System.out);
}

out.flush();
out.close();
in.close();
response.flushBuffer();

%>


-----output----

<?xml version="1.0" encoding="iso-8859-1"?>
<tcdt_input>
<parameter name='1'>
<value>t</value>
<value>t</value>
<value>tt</value>
</parameter>
<parameter name='2'>
<value>t</value>
<value>t</value>
<value>t</value>
</parameter>
</tcdt_input>
?
 
R

Ryan Dillon

You are writing out the -1 byte that indicates the end of file. Change
the loop to something like:

while ((bit = in.read()) >= 0) {
out.write(bit);
}


Regards
 
A

anilkumar05

Hi Ryan,

Thanks a lot.. changed the while loop and now its working fine.
once again thanks for correcting me..

Regards,
Anil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top