在文件下载的时候,文件名字长度超过17个汉字的时候,便会自动被截取缩短,而且还加A,B之类的字符,名字被改变,下载连接的文件名字无改变,打开就被改变,希望GG,JJ帮忙看下,小弟不胜感激。
下面是下载的Servlet.
package sanheoa;
import javax.servlet.*;
import javax.servlet.http.*;
import sanhe.xdatabase.XDBEncodeConvert;
import java.io.*;
import com.jspsmart.upload.SmartUpload;
import sanhe.XException;
public class Download extends HttpServlet {
public SmartUpload sUpload=null;
public String sourceFilePathName="";
public String namecn="";
//Initialize global variables
public void init() throws ServletException {
sUpload= new SmartUpload();
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
sourceFilePathName=XDBEncodeConvert.toChinese(request.getParameter("source"));
namecn = XDBEncodeConvert.toChinese(request.getParameter("name"));
String aimFile = request.getSession().getServletContext().getRealPath(sourceFilePathName);
String email = XDBEncodeConvert.toChinese(request.getParameter("mail"));
if(email.equals("1")){
response.reset();
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition","attachment; filename=\"" + toUtf8String(namecn) + "\"");//
try{
java.io.OutputStream os = response.getOutputStream();
java.io.FileInputStream fis = new java.io.FileInputStream(aimFile);
byte[] b = new byte[2048];
int i = 0;
while ((i = fis.read(b)) > 0){
os.write(b, 0, i);
}
fis.close();
os.flush();
os.close();
}
catch (Exception e){
e.printStackTrace();
}
}else{
try {
sUpload.initialize(this.getServletConfig(),request, response);
sUpload.setContentDisposition(null);
}
catch (ServletException ex) {
XException.write("aaa");
}
try {
sUpload.downloadFile(sourceFilePathName);
}
catch (Exception e) {
XException.write("sanhe.cdatebase.download.backgroundDispose:" + e.toString());
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
public void destroy() {
}
public static String toUtf8String(String s)
{
StringBuffer stringbuffer = new StringBuffer();
for(int i = 0; i < s.length(); i++)
{
char c = s.charAt(i);
if(c >= 0 && c <= '\377')
{
stringbuffer.append(c);
} else
{
byte abyte0[];
try
{
abyte0 = String.valueOf(c).getBytes("utf-8");
}
catch(Exception exception)
{
abyte0 = new byte[0];
}
for(int j = 0; j < abyte0.length; j++)
{
int k = abyte0[j];
if(k < 0)
{
k += 256;
}
stringbuffer.append("%" + Integer.toHexString(k).toUpperCase());
}
}
}
return stringbuffer.toString();
}
}
先谢谢大家了