当前位置:编程学习 > JAVA >>

普通上传文件,后台取得的中文文件名乱码?找不到原因

 用servlet使用DiskFileUpload做一个普通文件上传, 可是上传得到的文件名总是乱码,问题在哪里??

页面用的是html:

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>文件上传</title>

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <link rel="stylesheet" type="text/css" href="css/style.css">

  </head>
  
  <body>
     <form action="servlet/UploadServlet" method="post" enctype="multipart/form-data">
         <div align="center">
             <fieldset style="width:50%;">
                <legend>上传文件</legend><br/>
                <div class="line">
                    <div align="left" class="leftDiv">上传文件</div>
                    <div align="left" class="rightDiv">
                        <input type="file" name="file1" class="text">
                    </div>
                </div>
.........
 </fieldset>
</div>
</form>
  </body>
</html>


下面是servlet:

package com.sfd.servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;

public class UploadServlet extends HttpServlet{

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {
//....
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {

response.setCharacterEncoding("UTF-8");
//request.setCharacterEncoding("UTF-8");
response.setContentType("text/html");

DiskFileUpload diskFileUpload = new DiskFileUpload();
try {
List<FileItem> list = diskFileUpload.parseRequest(request);

for (FileItem fileItem : list) {

if (!fileItem.isFormField()) {  
if ("file1".equals(fileItem.getFieldName())) {

File remoteFile = new File(new String(fileItem.getName().getBytes(), "UTF-8"));

File file = new File(this.getServletContext().getRealPath("upload"),  remoteFile.getName());
// if (file1.exists()) {
// file1.delete();
// }

System.out.println("file_Path: " + file.toString());
file1.getParentFile().mkdirs();
file1.createNewFile();

InputStream ins = fileItem.getInputStream();
OutputStream ous = new FileOutputStream(file1);
byte[] buffer = new byte[1024];
int len = 0;
while((len = ins.read(buffer)) > -1) {
ous.write(buffer, 0, len);
}

}
}
}

} catch (Exception e) {
e.printStackTrace();
}
}

}



打印语句:
file_Path:   D:\Program Files\tomcat\webapps\servlet\attachment\δ????.jpg

servlet java DiskFileUpload  文件上传 乱码 --------------------编程问答-------------------- 看看你的tomcat吧,把connector的编码设置为utf-8试试。

<Connector port="8080" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443"  URIEncoding="UTF-8"               /> --------------------编程问答-------------------- 做项目 从来不需要修改 tomcat的,不知道 谁老是建议人家修改tomcat,万一人家 部署几个 工程呢,你修改了tomcat影响了别人怎么办。
建议楼主 servlet加过滤器 --------------------编程问答-------------------- 虽然几处编码都使用了UTF-8,但是感觉页面编码默认使用ISO-8859-1或者GBK。

所以可以参考我写的一篇文章进行测试,看是哪种情况。
根据很多项目案例下各种情况总结而来。

http://blog.csdn.net/nutony/article/details/7185043 --------------------编程问答-------------------- 把你的response.setCharacterEncoding("UTF-8");里面的UTF-8换成ISO-8859-1试试,下面那个地方也换一下。。。
--------------------编程问答-------------------- 应该是编码问题。。
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,