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 ...
随机推荐
- 梦想CAD控件安卓控件事件
MxDrawActivity.commandEvent 命令调用事件. 参数 说明 int iCommand 命令ID,这个ID用户自已来取的,只要多个命令ID不重复就可以 代码实现如下: publi ...
- 并发和多线程(三)--并发容器J.U.C和lock简介
AQS: 是AbstractQueuedSynchronizer的简称,JUC的核心 底层是sync queue双向链表,还可能有condition queue单向链表,使用Node实现FIFO队列, ...
- 以POST方式推送JSON数据,并接收返回的服务器消息
private static string GetResult(string jsonString, string type) { string url = GetUrl(type); string ...
- Python 字符串常见的用法
line = “ni hao wo jiao key” line.capotalize()#首字母大写 line.center(20)#居中显示固定的字符 line.count('n')#计数,计算该 ...
- 浅谈java浅拷贝和深拷贝
前言:深拷贝和浅拷贝的区别是什么? 浅拷贝:被复制的对象的所有变量都含有原来对象相同的值,而所有的对其他对象的引用仍然指向原来的对象.换言之, 浅拷贝仅仅复制所考虑的对象,而不复制它所引用的对象.深拷 ...
- spring 中属性scope 的prototype(有状态)和singleton(无状态)
默认情况下,从bean工厂所取得的实例为Singleton(bean的singleton属性) Singleton: Spring容器只存在一个共享的bean实例, 默认的配置. Prototype: ...
- <Zookeeper>入门 概念
分布式协调服务 Zookeeper 分布式环境的特点 1.分布性 基于一个硬件或者多个硬件设备以及多个软件组成的分布在不同网络计算机上的系统架构,通过消息传递进行通信协调.在空间上部署是可以任意的,网 ...
- react-router v4 学习实践
最近学习了 react-router v4,根据官方 API 文档和网上资源做了一个简单的路由示例. 先用官方的工具 create-react-app 初始化一个 react 项目模板,再根据自己的 ...
- buf.writeIntBE()函数详解
buf.writeIntBE(value, offset, byteLength[, noAssert]) buf.writeIntLE(value, offset, byteLength[, noA ...
- jsonview插件的常见使用方法整理
Jsonview是目前最热门的一款开发者工具插件,确切的来说jQuery JSONView是一款非常实用的格式化和语法高亮JSON格式数据查看器jQuery插件.它是查看json数据的神器. 下载地址 ...