Jsp program download problems

Source: Internet
Author: User


I downloaded the jsp (preferred for SUN Enterprise Applications) smartupload class, and then compiled the download program, as shown below:
<% @ Page contentType = "text/html; charset = gb2312" %> <% @ page language = "java" import = "com. jsp (preferred for SUN Enterprise Applications) smart. upload. * "%> <jsp (preferred for SUN Enterprise Applications): useBean id =" mySmartUpload "scope =" page "class =" com. jsp (preferred for SUN Enterprise Applications) smart. upload. smartUpload "/> <% mySmartUpload. initialize (pageContext );
MySmartUpload. downloadFile ("d: \ 111.txt ";
%>
After the script is run, the content in my txt file is directly displayed on the page. The zip file is the same, but garbled. What can I do without displaying it, just like downloading?

Eclipse reply: 10:54:22
It should be a problem with the SmartUpload class. jsp (the first choice for SUN Enterprise Applications) is no problem. I feel

Huangmw replied to: 11:27:17
This class is downloaded from the smart.com site of www. jsp (preferred for SUN enterprise-level applications). Isn't it a problem with their coding?
Can you provide me with a similar category? Thank you.

Eclipse reply: 12:11:30
Example of the TestFileDownload. jsp page (preferred for SUN Enterprise Applications:

<%
// Obtain the file name and path.
String filename = ”MengxianhuiDocTest.doc ";
String filepath = "D :\";

// Set the response header and download and save the file name
Response. setContentType ("APPLICATION/OCTET-STREAM ");
Response. setHeader ("Content-Disposition ",
"Attachment; filename =" + filename + """);

// Open the stream information of the specified file
Java. io. FileInputStream fileInputStream =
New java. io. FileInputStream (filepath + filename );

// Write the stream information
Int I;
While (I = fileInputStream. read ())! =-1 ){
Out. write (I );
}
FileInputStream. close ();
Out. close ();
%>

It is worth noting that in the content of the file to be downloaded, except the content of the file, no other characters should be appended, including spaces and carriage return line breaks. Sometimes, when writing code, to make the code clear and readable, some spaces, tabs, or carriage return line breaks are often added. Although this looks clear, sometimes the correct results may not be obtained. For example:
<% @ Page import = "java. io .*"
%> <Jsp (preferred for SUN Enterprise Applications): useBean id = "MyBeanFromMengxianhui" scope = "page"
Class = "com. Mengxianhui. DownloadBean"/>
It should be written as follows:
<% @ Page import = "java. io .*"
%> <Jsp (preferred for SUN Enterprise Applications): useBean id = "MyBeanFromMengxianhui" scope = "page"
Class = "com. Mengxianhui. DownloadBean"/>

Eclipse reply: 12:13:21
Note: APPLICATION/OCTET-STREAM is set download type

To change to your actual type, for example, excel should be written as application/vnd. ms-excel


Eclipse reply: 12:17:39
If you do not need jsp (the preferred choice for SUN Enterprise Applications) smart, you need to understand the encoding method of the browser, and you can decode it only when it is uploaded to the server. To obtain information about the uploaded file. See the following code.
Package mshtang. fileUpload;
Import java. io .*;
/** A class for storing file information, including the file name (String ),
** Field name (String), Content-Type (String), and Content (byte [])
** A void saveTo (File f) function is also provided to directly save the File content to a File)
** You can call an appropriate method in class {@ link ContentFactory} to generate an instance of this class.
** @ See ContentFactory
** @ See ContentFactory # getFileParameter
** @ See ContentFactory # getFileParameterValues
**/

Public class FileHolder
{
String contentType;
Byte [] buffer;
String fileName;
String parameterName;

FileHolder (byte [] buffer, String contentType, String fileName, String parameterName)
{
This. buffer = buffer;
This. contentType = contentType;
This. fileName = fileName;
This. parameterName = parameterName;
}
/** Save the object content to the specified object,
** <B> this method does not check whether the file is writable or already exists. </B>
** @ Param file: target file
** @ Throws the IOException thrown in the I/O operation
**/
Public void saveTo (File file) throws IOException
{
BufferedOutputStream out = new BufferedOutputStream (new FileOutputStream (file ));
Out. write (buffer );
Out. close ();
}

/** Save the object content to the specified object,
** <B> this method does not check whether the file is writable or already exists. </B>
** @ Param name: target file name
** @ Throws the IOException thrown in the I/O operation
**/
Public void saveTo (String name) throws IOException
{
SaveTo (new File (name ));
}

/**
** Returns the byte array of the content of a file.
** @ Return a byte array representing the File Content
**/
Public byte [] getBytes ()
{
Return buffer;
}

/**
** Return the name of the file before the file is uploaded on the client.
** @ Return the name of the file on the client before the file is uploaded
**/
Public String getFileName ()
{
Return fileName;
}

/**
** The Content-Type of the file is returned.
** @ Return Content-Type of the file
**/
Public String getContentType ()
{
Return contentType;
}

/**
** When a file is uploaded, the name attribute of the file control in the Html page form is returned.
** @ Return: when the object is uploaded, the name attribute of the file control in the Html page form
**/
Public String getParameterName ()
{
Return parameterName;
}
}

Eclipse replied to: 2002-10-18 12: 22:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.