Completely solve Chinese name file download and download file content garbled problem

Source: Internet
Author: User
Tags file size file upload string xsl xsl file
Solution | problem | download | chinese

Before, wrote a download.jsp file, can solve the download file garbled problem (such as: Doc,xsl file, etc.).
Later found, encountered in the Chinese name of the file, the file download will be an error ~ ~ ~ ~
Today, by rewriting the original download.jsp file has completely solved the problem ~
Now, put a whole set of files to upload the download method to paste out ~ ~ ~ for everyone to learn from!~!~!~!~!
Pharaoh of Ancient Egypt
--------------------------------------------------------------------------------------------------------------- ----
test environment: WEBLOGIC 8.1,win XP sp4,ie 6.0
-----------------------------------------------------
File Upload:
-----------------------------------------
Prepare for work: Import the famousSmartupload.jarComponent Packages
upload.jsp File
---------------------------------------------------------
<%@ page contenttype= "text/html; charset=gb2312 "%>
<%
Request.setcharacterencoding ("gb2312"); This sentence is very important, otherwise encountered in Chinese will be wrong ~
%>
<HTML><HEAD><TITLE> Upload </TITLE>
<meta content= "text/html; charset=gb2312 "http-equiv=content-type>
</HEAD>
<body leftmargin=0 topmargin=0>
<table width= "100%" height= "100%" border= "0" cellpadding= "0" cellspacing= "0" bgcolor= "#DEE7EF" >
<tr>
&LT;TD align= "center" >
<form action= "upload_ok.jsp" method=post name= "upload" enctype= "Multipart/form-data" >
<br>
Please enter the path of the attachment file <font color=red> * </FONT> for required items <br>
<br>
<table width= "317" border=0 cellpadding=0>
<TBODY>
<TR>
&LT;TD align=right valign=center nowrap> attachment path:</td>
<td><input type= "File" name= "file" style= "border:1px #FFFFFF solid;background: #efefef" > <font color= Red>*</font></td>
</TR>
&LT;TR align= "center" >
&LT;TD height=60 colspan= "2" Valign=center nowrap> <input style= "height:22px" name=b1 type=submit-value= "OK" >
<input style= "height:22px" name=b2 type=reset value= "Cancel" >
</TD>
</TR>
</TBODY>
</TABLE>
</FORM>
</td>
</tr>
</table>
</BODY></HTML>
---------------------------------------------------------
upload_ok.jsp File
---------------------------------------------------------
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "com.jspsmart.upload.*"%>
<HTML><HEAD><TITLE> Upload Success!</title>
<meta content= "text/html; charset=gb2312 "http-equiv=content-type>
</HEAD>
<body leftmargin=0 topmargin=0>
<jsp:usebean id= "mysmartupload" scope= "page" class= "Com.jspsmart.upload.SmartUpload"/>
<table width= "80%" border= "0" cellpadding= "0" cellspacing= "0" bgcolor= "#DEE7EF" >
<tr>
&LT;TD align= "center" >
<%
int count=0;
String fileName = null;
Mysmartupload.initialize (PageContext);
Mysmartupload.upload ();
Com.jspsmart.upload.File myFile = Mysmartupload.getfiles (). GetFile (0);
if (!myfile.ismissing ()) {
String Ext=myfile.getfileext ();//Get suffix
FileName = Myfile.getfilename ();
Myfile.saveas ("/files/" + fileName);//You want to keep the relative path to the folder where the file resides
Out.println ("File:<b>" +filename+ </b> upload success!<br> file size: "+ myfile.getsize () +" kb<br> ");
}
%>
</BODY></HTML>
---------------------------------------------------------

File Download:
-----------------------------------------
Example of a hyperlink to a file:
<% String fname = "Chinese test. Xsl"; Suppose your file name is: Chinese test. xsl
%>
<a target= "_blank" href= "download.jsp?filename=<%=fname%>" > Download </A>
Example of a hyperlink to a file-2 re-encoding the filename with utf-8:
<%@ page contenttype= "text/html;charset=gb2312" session= "true"%>
<% String Name=java.net.urlencoder.encode ("World culture. Doc", "UTF-8"));%> <a href= "c:\<%=name%>" > World Cultural .doc</a>

download.jsp File
---------------------------------------------------------
<%
Java.io.BufferedInputStream Bis=null;
Java.io.BufferedOutputStream Bos=null;
try{
String filename=request.getparameter ("filename");
Filename=new String (filename.getbytes ("iso8859-1"), "gb2312");
Response.setcontenttype ("Application/x-msdownload");
Response.setheader ("Content-disposition", "Attachment Filename=" +new String (Filename.getbytes ("gb2312"), " Iso8859-1 "));
bis =new java.io.BufferedInputStream (New Java.io.FileInputStream (Config.getservletcontext (). Getrealpath ("files/" + filename)));
Bos=new Java.io.BufferedOutputStream (Response.getoutputstream ());
byte[] buff = new byte[2048];
int bytesread;
while ( -1!= (bytesread = bis.read (buff, 0, buff.length))) {
Bos.write (Buff,0,bytesread);
}
}
catch (Exception e) {
E.printstacktrace ();
}
finally {
if (bis!= null) bis.close ();
if (BOS!= null) bos.close ();
}
%>



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.