Java servlet实现的文件下载,下载的名字为中文,文件内容就为空?
也就是说,中文他就识别不出正确路径。我的文件名称是:currentTime+"_"+filename,(filename为中文,例如:1418192996741_中文名....
也就是说,中文他就识别不出正确路径。我的文件名称是:currentTime+"_"+filename,(filename为中文,例如:1418192996741_中文名.txt)
路径:D:/project/workspace/Phone/WebContent/sdnfile/download/
public void doGet(HttpServletRequest request,
HttpServletResponse response)
{
String aFilePath = null; //要下载的文件路径
String aFileName = null; //要下载的文件名
FileInputStream in = null; //输入流
ServletOutputStream out = null; //输出流
String filename = SdnUploadServlet.filename;//上传文件名
String currentTime = Long.toString(SdnUploadServlet.currentTime);
byte[] a;
try {
aFilePath = "D:/project/workspace/Phone/WebContent/sdnfile/download/";
//先URLENcode一下,在DEcode,我是这样想的,但程序不识别解码后的文件名,就会自己新建一个新的空白文件
aFileName = currentTime+"_"+filename;//java.net.URLEncoder.encode(currentTime+"_"+filename, "utf-8");
a = aFileName.getBytes("utf-8");
String temp = java.net.URLEncoder.encode(new String(a, "utf-8"), "utf-8");
temp = StringUtils.replace(temp, "+", "%20");
aFileName = java.net.URLEncoder.encode(temp, "utf-8");
} catch (UnsupportedEncodingException e1) {…………略……………………
try
{response.setContentType("text/plain;charset=UTF-8");
response.setHeader("Content-disposition", "attachment; filename="+ java.net.URLDecoder.decode(aFileName,"UTF-8"));
in = new FileInputStream(aFilePath + java.net.URLDecoder.decode(aFileName,"UTF-8"));
System.out.println(aFilePath + aFileName);
out = response.getOutputStream();
}
…………略…………………… 展开
路径:D:/project/workspace/Phone/WebContent/sdnfile/download/
public void doGet(HttpServletRequest request,
HttpServletResponse response)
{
String aFilePath = null; //要下载的文件路径
String aFileName = null; //要下载的文件名
FileInputStream in = null; //输入流
ServletOutputStream out = null; //输出流
String filename = SdnUploadServlet.filename;//上传文件名
String currentTime = Long.toString(SdnUploadServlet.currentTime);
byte[] a;
try {
aFilePath = "D:/project/workspace/Phone/WebContent/sdnfile/download/";
//先URLENcode一下,在DEcode,我是这样想的,但程序不识别解码后的文件名,就会自己新建一个新的空白文件
aFileName = currentTime+"_"+filename;//java.net.URLEncoder.encode(currentTime+"_"+filename, "utf-8");
a = aFileName.getBytes("utf-8");
String temp = java.net.URLEncoder.encode(new String(a, "utf-8"), "utf-8");
temp = StringUtils.replace(temp, "+", "%20");
aFileName = java.net.URLEncoder.encode(temp, "utf-8");
} catch (UnsupportedEncodingException e1) {…………略……………………
try
{response.setContentType("text/plain;charset=UTF-8");
response.setHeader("Content-disposition", "attachment; filename="+ java.net.URLDecoder.decode(aFileName,"UTF-8"));
in = new FileInputStream(aFilePath + java.net.URLDecoder.decode(aFileName,"UTF-8"));
System.out.println(aFilePath + aFileName);
out = response.getOutputStream();
}
…………略…………………… 展开
若以下回答无法解决问题,邀请你更新回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |