1. request.setCheracterEncoding("utf-8");
    2. DiskFileUpload.setHeaderEncoding("utf-8");
    3. 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()) {// 文件名 &nbsp; 表单文本项
      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乱码的更多相关文章

  1. WEB文件上传之apache common upload使用(一)

    文件上传一个经常用到的功能,它有许多中实现的方案. 页面表单 + RFC1897规范 + http协议上传 页面控件(flash/html5/activeX/applet) + RFC1897规范 + ...

  2. .NET Core System.Drawing.Common 中文乱码的坑

    最近在写一个汉字取点阵的程序,最开始是在win环境下运行的,没发现什么异常,然后今天把程序放在centos 下后发现英文正常,中文完全变成两位的字了,最开始是字体的原因 在把宋体等安装到centos ...

  3. Java 关于中文乱码处理的经验总结【转载】

    为什么说乱码是中国程序员无法避免的话题呢?这个首先要从编码机制上说起,大家都是中文和英文的编码格式不是一样,解码也是不一样的!如果中国的程序员不会遇到乱码,那么只有使用汉语编程.汉语编程是怎么回事我也 ...

  4. JAVA-----乱码的处理 乱码的解决方法总结

    为什么说乱码是程序员无法避免的话题呢?这个首先要从编码机制上说起,大家都是中文和英文的编码格式不是一样,解码也是不一样的!工作遇到各种各样的乱码的解决方法总结一下. 对于Java由于默认的编码方式是 ...

  5. servlet上传下载(任何格式的都可以)

    jar不能低于此版本,JDK1.6以上,否则户报错 <dependency> <groupId>commons-fileupload</groupId> <a ...

  6. Spring MVC学习

    SpringMVC框架 转载请注明出处 目录 一:配置springMVC开发环境 1.1.配置文件的helloworld 1.2.基于注解的helloworld 二:@RequestMapping详解 ...

  7. Qt控件样式 Style Sheet Demo

    迟来的笔记,作为一个程序员每日记事已养成习惯,离开许久,不知不觉已喜欢用文字表达对技术的热爱,学无止境! Qt – 一个跨平台应用程序和UI开发框架:它包括跨平台类库.集成开发工具和跨平台 IDE,使 ...

  8. Web Api 与 Andriod 接口对接开发经验

    最近一直急着在负责弄Asp.Net Web Api 与 Andriod 接口开发的对接工作! 刚听说要用Asp.Net Web Api去跟 Andriod 那端做接口对接工作,自己也是第一次接触Web ...

  9. html5中上传图片

    从相册中选择图片上传 function uploadFromAlbum(type) { var dirtype = ""; if ("pick_store_license ...

随机推荐

  1. CAD控件:控件图形数据库概要说明

    1.1 控件数据库 3 1.1.1 数据库概述 3 1.2 数据库初始化 4 1.3 创建和组织数据库 4 1.4 保存数据库 4 1.5 插入一个数据库 4 1.6 设置当前数据库值 5 1.6.1 ...

  2. JavaScipt30(第五个案例)(主要知识点:flex布局)

    承接上文,这是第5个案例:这节没什么讲的,随便记录下吧,主要是用了flex布局与transform translateY,js部分和案例1类似. 附上项目链接: https://github.com/ ...

  3. 牛客练习赛25 C 再编号

    解题思路 我们先来观察一下题目中给出的公式 $$a'_i=(\sum_{j=1}^na_j)-a_i$$ 通过这个公式推一下经过再编号后的序列的总和,因为我们推出这个和之后可以进行下一次计算. $$\ ...

  4. Linux之iptables(六、rich规则)

    其它规则 当基本firewalld语法规则不能满足要求时,可以使用以下更复杂的规则 rich-rules 富规则,功能强,表达性语言 Direct configuration rules 直接规则,灵 ...

  5. apache 添加虚拟机

    <VirtualHost *:80> DocumentRoot "E:/UPUPW_AP7.0/htdocs/xd.local/public" ServerName a ...

  6. [bzoj4521][Cqoi2016][手机号码] (数位dp+记忆化搜索)

    Description 人们选择手机号码时都希望号码好记.吉利.比如号码中含有几位相邻的相同数字.不含谐音不 吉利的数字等.手机运营商在发行新号码时也会考虑这些因素,从号段中选取含有某些特征的号 码单 ...

  7. java Beanutils.copyProperties( )用法

    这是一篇开发自辩甩锅稿~~~~ 昨天测试小姐姐将我的一个bug单重开了,emmmm....内心OS:就调整下对象某个属性类型这么简单的操作,我怎么可能会出错呢,一定不是我的锅!!but再怎么抗拒,bu ...

  8. 九度oj 题目1046:求最大值

    题目1046:求最大值 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:11782 解决:4789 题目描述: 输入10个数,要求输出其中的最大值. 输入: 测试数据有多组,每组10个数. ...

  9. MVC系统学习2—MVC路由

    在MVC下不是通过对物理文件的映射来实行访问的,而是通过定义后的路由Url来实现访问的.在前一篇讲到我们是在全局文件下进行路由配置. routes.MapRoute(                & ...

  10. 夜话JAVA设计模式之适配器模式(adapter pattern)

    适配器模式:将一个类的接口,转换成客户期望的另一个接口,让不兼容的接口变成兼容. 1.类适配器模式:通过多重继承来实现适配器功能.多重继承就是先继承要转换的实现类,再实现被转换的接口. 2.对象适配器 ...