common upload乱码
- request.setCheracterEncoding("utf-8");
- DiskFileUpload.setHeaderEncoding("utf-8");
- FileItem.getString("utf-8");
上面这三个地方设置好之后就应该解决了。package onlineTest.server; import java.io.File;
import java.io.IOException;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import onlineTest.bean.User;
import onlineTest.dao.UserDao; import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload; public class ReceiveFile extends HttpServlet { private String uploadPath = "uploadpic/upload/"; // 上传文件的目录
private String tempPath = "uploadpic/uploadtmp/"; // 临时文件目录
/* private String serverPath = null; */
private String[] fileType = new String[] { ".jpg", ".gif", ".bmp", ".png",".jpeg", ".ico" };
private int sizeMax = 5;// 图片最大上限 @Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
// 服务器端根目录
String serverPath = getServletContext().getRealPath("/").replace("\\","/");
System.out.println("地址+" + serverPath);
// Servlet初始化时执行,如果上传文件目录不存在则自动创建
if (!new File(serverPath + uploadPath).isDirectory()) {
new File(serverPath + uploadPath).mkdirs();
}
if (!new File(serverPath + tempPath).isDirectory()) {
new File(serverPath + tempPath).mkdirs();
} DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(5 * 1024); // 最大缓存
factory.setRepository(new File(serverPath + tempPath));// 临时文件目录 ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(sizeMax * 1024 * 1024);// 文件最大上限
upload.setHeaderEncoding("utf-8");
String filePath = null;
try {// 解析request对象中的表单项
List<FileItem> items = upload.parseRequest(request);// 获取所有文件列表 ; list中是FileItem对象
// 提取文本
for (int i = 0; i < items.size(); i++) {
// 里面一个for循环,获取一行的数据
FileItem item = items.get(i); if (!item.isFormField()) {// 文件名 表单文本项
String fileName = item.getName().toLowerCase();
item.getString("utf-8");
if (fileName.endsWith(fileType[0])
|| fileName.endsWith(fileType[1])
|| fileName.endsWith(fileType[2])
|| fileName.endsWith(fileType[3])
|| fileName.endsWith(fileType[4])
|| fileName.endsWith(fileType[5])) {
filePath = serverPath + uploadPath + fileName; // System.out.println(filePath);
File file = new File(filePath);
item.write(file);
System.out.println("fileName+" + fileName);
System.out.println("filePath+" + filePath);
System.out.println(getClass().getResource("/").getFile().toString()); User u =new User();
u=(User)request.getSession().getAttribute("user");
u.setUrl(fileName);
int result = UserDao.addFavicon(u);
if (result==0) {
request.setAttribute("errorMsg",
"上传失败,请确认上传的文件存在并且类型是图片!");
} else {
request.setAttribute("errorMsg", "上传成功,请右键点击刷新(或者按F5)");
}
request.getRequestDispatcher(
"web/common/show/uploaderror.jsp").forward(
request, response);
} else {
request.setAttribute("errorMsg",
"上传失败,请确认上传的文件存在并且类型是图片!");
request.getRequestDispatcher(
"web/common/show/uploaderror.jsp").forward(
request, response);
}
} else {
// 非文件流
String value = item.getString();
value = new String(value.getBytes("ISO-8859-1"), "UTF-8");
// System.out.println(value);
System.out.println(value);
} }
} catch (Exception e) {
e.printStackTrace();
request.setAttribute("errorMsg", "上传失败,请确认上传的文件存在并且类型是图片!");
request.getRequestDispatcher("web/common/show/uploaderror.jsp")
.forward(request, response);
}
} @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doPost(req, resp);
}
}
common upload乱码的更多相关文章
- WEB文件上传之apache common upload使用(一)
文件上传一个经常用到的功能,它有许多中实现的方案. 页面表单 + RFC1897规范 + http协议上传 页面控件(flash/html5/activeX/applet) + RFC1897规范 + ...
- .NET Core System.Drawing.Common 中文乱码的坑
最近在写一个汉字取点阵的程序,最开始是在win环境下运行的,没发现什么异常,然后今天把程序放在centos 下后发现英文正常,中文完全变成两位的字了,最开始是字体的原因 在把宋体等安装到centos ...
- Java 关于中文乱码处理的经验总结【转载】
为什么说乱码是中国程序员无法避免的话题呢?这个首先要从编码机制上说起,大家都是中文和英文的编码格式不是一样,解码也是不一样的!如果中国的程序员不会遇到乱码,那么只有使用汉语编程.汉语编程是怎么回事我也 ...
- JAVA-----乱码的处理 乱码的解决方法总结
为什么说乱码是程序员无法避免的话题呢?这个首先要从编码机制上说起,大家都是中文和英文的编码格式不是一样,解码也是不一样的!工作遇到各种各样的乱码的解决方法总结一下. 对于Java由于默认的编码方式是 ...
- servlet上传下载(任何格式的都可以)
jar不能低于此版本,JDK1.6以上,否则户报错 <dependency> <groupId>commons-fileupload</groupId> <a ...
- Spring MVC学习
SpringMVC框架 转载请注明出处 目录 一:配置springMVC开发环境 1.1.配置文件的helloworld 1.2.基于注解的helloworld 二:@RequestMapping详解 ...
- Qt控件样式 Style Sheet Demo
迟来的笔记,作为一个程序员每日记事已养成习惯,离开许久,不知不觉已喜欢用文字表达对技术的热爱,学无止境! Qt – 一个跨平台应用程序和UI开发框架:它包括跨平台类库.集成开发工具和跨平台 IDE,使 ...
- Web Api 与 Andriod 接口对接开发经验
最近一直急着在负责弄Asp.Net Web Api 与 Andriod 接口开发的对接工作! 刚听说要用Asp.Net Web Api去跟 Andriod 那端做接口对接工作,自己也是第一次接触Web ...
- html5中上传图片
从相册中选择图片上传 function uploadFromAlbum(type) { var dirtype = ""; if ("pick_store_license ...
随机推荐
- java 23种设计模式 链接集锦
创建型抽象工厂模式 http://www.cnblogs.com/java-my-life/archive/2012/03/28/2418836.html工厂方法 http://www.cnblogs ...
- 魂酥的LNOI2019滚粗记
$Day -???$ 高一下终于开始了 在开学文化课考试的水题之下混了个(成绩)前排 于是我便油然而生一种自信 我!要!进!省!队! 讲句真话我这么想的时候连自己都觉得自己是个十足的沙雕 我又不是zw ...
- Spring Boot之简单的MVC
最近开始看Spring Boot,发现其开发起来真是方便.今天就来实现一个简单的Spring MVC 请求,纯Java代码的哦. 1.Maven必不可少,先看看都加载了那些依赖: <?xml v ...
- Rsync远程同步工具使用
rsync远程同步工具使用 Rsync(remote synchronize) 是一个远程数据同步工具,可以使用"Rsync算法"同步本地和远程主机之间的文件.Rsync的好处是只 ...
- Network----轮询
轮询: 定时每隔多长时间刷新一次,但是,7X24的对服务器的压力会过大,因为在夜间或者是流量低峰期时,他还要持续工作. 客户端发一次请求,服务器就要相应一次. 长轮询: 和轮询的模式不同,长轮询是一次 ...
- CKplayer:视频推荐和分享插件设置
如果想去掉播放结束后显示精彩视频推荐的插件,可以打开ckplayer.js和ckplayer.xml,找到以下代码,然后注释掉即可: control_rel: 'related.swf,ckplaye ...
- 【Codeforces 1102E】Monotonic Renumeration
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 会发现如果a[i]=a[j] 那么b[i]~b[j]都是相同的,等于b[i] 而b[i]等于b[i-1]+1或者b[i] 有两种可能 所以对于 ...
- Android layer-list(3)
Android layer-list(3) 在附录文章3.4的基础上,就Android layer-list再写一个较为复杂的应用. 先写布局文件,该布局涉及到LinearLayoutCompa ...
- Junit4使用总结
常用注解 @Before:初始化方法,在任何一个测试方法执行之前,必须执行的代码. @After:释放资源,在任何一个测试方法执行之后,需要进行的收尾工作. @Test:测试方法,表明这是一个测试 ...
- linux 创建 bootable iso 文件
windows制作iso文件通过ultraiso可以实现. linux下用mkisofs这个命令就可以 最简单的用法 mkisofs -o target.iso source 要制作可以启动的iso文 ...