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 ...
随机推荐
- CLISTCTRL2
回顾: 刚刚写完,因为是分期写的,所以最初想好好做一下的文章格式半途而废了~说的也许会有点啰嗦,但是所有的基础用到的技术细节应该都用到了. 如果还有什么疑问,请回复留言,我会尽力解答. 如果有错误,请 ...
- Vue指令2:v-bind
v-bind 指令可以更新 HTML 属性: <a v-bind:href="url">...</a> 在这里 href 是参数,告知 v-bind 指令将 ...
- mac系统,鼠标移动太慢
to check your speed: defaults read -g com.apple.mouse.scaling to set your speed defaults write -g co ...
- 梦想CAD控件安卓界面控制
CAD控件界面上所有元素都可以控制显示或隐藏,下面将逐一介绍详细用法. 设置工具文件 MxFunction.setToolFile 设置工具文件.详细说明如下: 参数 说明 String sFile ...
- ThinkPHP---TP功能类之邮件
[一]概论 (1)简介: 这里说的邮件不是平时说的email邮件(邮件地址带有@符号的),而是指的一般论坛网站的站内信息,也叫私信或者pm(private message私信) [二]站内信案例 (1 ...
- Oracle RAC 后台进程
LMS - Gobal 全局缓存服务进程 LMD - Global Enqueue Service Daemon 全局查询服务守护进程 LMON - 全局服务器监控进程 LCK0 ...
- idea之快速查看类所在jar包
- 实战:tcp链接rst场景tcpdump分析
RST为重置报文段,它会导致TCP连接的快速拆迁,且不需要ack进行确认. 1.针对不存在的端口的连请求 客户端: #include <unistd.h> #include <sys ...
- [Algorithm] 9. Two Sum
Description Given an array of integers, return indices of the two numbers such that they add up to a ...
- 创建和获取cookie
创建和获取cookie 制作人:全心全意 cookie:在互联网中,cookie是小段的文本信息,在网络服务器上生成,并发送给浏览器.通过使用cookie可以标识用户身份,记录用户名和密码,跟踪重复用 ...