Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

download files from server

Status
Not open for further replies.

MoreFeo

Technical User
Nov 29, 2002
547
ES
Hi, I'm trying to follow thread thread695-824677 instructions, and it seems to work ok, except that the file created is 20 kB bigger than the original, and that files that are not plain text (like .doc or .zip) can't be opened.

Does anyone know what can cause this?

Thanks
 
Sorry, it's not 20kB, it's 20 bytes that it adds, I think at the begining of the file.

Here is the code I've tried:

Code:
<netui-data:getData resultId="path" value="{pageFlow.filePath}" />
<netui-data:getData resultId="name" value="{pageFlow.fileName}" />
<%
    String path = (String)pageContext.getAttribute("path");
    String name = (String)pageContext.getAttribute("name");
    
    response.setContentType("application/octet-stream");
    response.setHeader("Content-disposition", "attachment;filename="+name);
    
    File myFile = new File(path);
    OutputStream o = response.getOutputStream();
    
    FileInputStream fis = new FileInputStream(myFile);
    int i = fis.available();
    System.out.println("fis.available: " + i);
    int iRead;
    int iLength = 0;
    while ((iRead=fis.read())!=-1)
    {
        o.write(iRead);
        iLength++;
    }
    System.out.println("iLength: " + iLength);
    fis.close();
    o.flush();
    o.close();
%>

When I execute this, both fis.available and iLength give the current size of the file, but the file created is 20 bytes bigger and is corrupt.

If I try this with a plain text file, I can see that it adds 10 blank lines at the begining of the file.

Does anyone know how to fix this?

Thanks.
 
Well, I got it.

I was doing it in a JSP page, but it must be done from a servlet, because JSPs write more code than you want (each <%@ import inserts a blank line).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top