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

vrenzhuv 2013-01-11 06:40:23
用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

...全文
1358 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
_沉浮_ 2015-05-21
  • 打赏
  • 举报
回复
这情况应该是发送的请求出现乱码,那应该设置request.setCharacterEncoding("utf-8")
sinat_14935775 2015-05-21
  • 打赏
  • 举报
回复
直接把request的字符改成utf-8,或者加字符过滤器,还可以在tomcat的server.xml里面添加URIEncoding = "UTF-8"
「已注销」 2015-05-21
  • 打赏
  • 举报
回复
一样的问题,没找到原因。
Jinggg_ 2015-05-21
  • 打赏
  • 举报
回复
response.setContentType("text/html","charset=utf-8");
kyolxs 2013-01-18
  • 打赏
  • 举报
回复
应该是编码问题。。
wyatou 2013-01-18
  • 打赏
  • 举报
回复
把你的response.setCharacterEncoding("UTF-8");里面的UTF-8换成ISO-8859-1试试,下面那个地方也换一下。。。
长安紫薯 2013-01-14
  • 打赏
  • 举报
回复
虽然几处编码都使用了UTF-8,但是感觉页面编码默认使用ISO-8859-1或者GBK。 所以可以参考我写的一篇文章进行测试,看是哪种情况。 根据很多项目案例下各种情况总结而来。 http://blog.csdn.net/nutony/article/details/7185043
chenluozhi 2013-01-12
  • 打赏
  • 举报
回复
做项目 从来不需要修改 tomcat的,不知道 谁老是建议人家修改tomcat,万一人家 部署几个 工程呢,你修改了tomcat影响了别人怎么办。 建议楼主 servlet加过滤器
balance9 2013-01-11
  • 打赏
  • 举报
回复
看看你的tomcat吧,把connector的编码设置为utf-8试试。 <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" />

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧