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. SQL删除重复数据(根据多个字段),pandas的nan存入数据库报错

    delete from M_FACTOR_DATA_TEST a where (a.factor_id,a.data_date,a.stock_code) in (select factor_id,d ...

  2. cgroup代码浅析(2)

    info include/linux/memcontrol.h memcg相关的函数 数据结构 mem_cgroup在每个node下,都有一个lruvec, 这个lruvec保存在mem_cgroup ...

  3. 洛谷——P1349 广义斐波那契数列(矩阵加速)

    P1349 广义斐波那契数列 题目描述 广义的斐波那契数列是指形如$an=p\times a_{n-1}+q\times a_{n-2}$?的数列.今给定数列的两系数$p$和$q$,以及数列的最前两项 ...

  4. POJ2454——Jersey Politics

    POJ2454——Jersey Politics 题目大意: 在泽西奶牛和荷斯坦奶牛的最新普查中,威斯康星奶牛在谷仓中获得了三个档位. 泽西奶牛目前控制着国家重新分配委员会. 他们想将国家分为三个相当 ...

  5. Linux学习笔记(二) 文件管理

    了解 Linux 系统基本的文件管理命令可以帮助我们更好的使用 Linux 系统,以下介绍几个常用的文件管理命令 1.pwd pwd 是 Print Working Directory 的简写,用于显 ...

  6. Swoft 新手向教程 - 通过 Docker 搭建一个开发环境

    本系列文章将从使用层面介绍 Swoft 框架的使用及业务开发,面向初中级的 PHPer Swoft首个基于 Swoole 原生协程的新时代 PHP 高性能协程全栈组件化框架,内置协程网络服务器及常用的 ...

  7. 有哪些可以节省chrome内存的扩展插件?

    不知道从什么时候开始,chrome浏览器就这样不知不觉的超过IE浏览器成为全球第一大浏览器.我们在赞赏chrome浏览器流畅的速度时,更多的是对其chrome插件功能的赞赏.但是我们也发现了一个致命的 ...

  8. PAT 1123 Is It a Complete AVL Tree

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

  9. Unity常用常找(二)

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/51315050 作者:car ...

  10. python整数转ASCII码

    # *-* coding:utf-8 *-* import binascii data = [1441465642, 251096121, -870437532, -944322827, 647240 ...