1、文件内容全是乱码
2、filename=” “是乱码
3、controller中日志显示request.getCharaterEncoding()=null
网上已经有很多了,给出参考文章,自行查看,这里不再赘述,只给出大体流程及思路。后面贴出我项目中文件上传部分的代码
文件上传、设置文件解析器、设置字符编码、上传至服务器虚拟路径等参考文章如下:
1、文件上传至本地磁盘:https://blog.csdn.net/yerenyuan_pku/article/details/72511975
2、springmvc文件上传三种方式:https://www.cnblogs.com/fjsnail/p/3491033.html
3、文件上传将相对路径存储至数据库:https://blog.csdn.net/qq_33764491/article/details/77440180
4、文件上传与下载:http://www.cnblogs.com/WJ-163/p/6269409.html
5、表单数据与文件同时提交:https://blog.csdn.net/u012383839/article/details/73028695
6、配置文件解析器:https://blog.csdn.net/qq_37432056/article/details/74999002
针对乱码、filename为空、已经设置了字符编码为空但后台显示为null的解决思路:
1、springmvc文件上传需要设置对应的文件解析器(springmvc.xml中)
2、引入jar包,这两个是采用springmvc内置上传功能方式(第三种)必须的
com.springsource.org.apache.commons.fileupload-1.2.0.jar
com.springsource.org.apache.commons.io-1.4.0.jar
3、设置文件解析器(包含文件上传大小、字符编码)
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="5242880" />
<property name="maxInMemorySize" value="4096" />
<property name="defaultEncoding" value="UTF-8">property>
bean>
注意:bean的id一定要有,而且只能为这个名称。例如:写成multipartResolver_1就会报错
4、jsp上传页设置字符编码、web.xml配置过滤器、检查项目默认编码
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<filter>
<filter-name>encodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
param>
<param-name>encodingparam-name>
<param-value>utf-8param-value>
param>
param>
<param-name>forceEncodingparam-name>
<param-value>trueparam-value>
param>
filter>
<filter-mapping>
<filter-name>encodingFilterfilter-name>
/*
经过以上设置:基本不会出现乱码,如果后台获取文件名(originalFileName = mulFile.getOriginalFilename();)显示空指针。请设置@RequestParam注解(@RequestParam(value=”location”,required=false)
/**
* 文件上传
* @param session
* @return
*/
@RequestMapping(value="/sUpload",method = RequestMethod.POST)
public void sUpload(@RequestParam(value="location",required=false) MultipartFile mulFile,HttpServletRequest request,HttpServletResponse response,HttpSession session) {
//TODO
}
1、效果
3、controller
/**
* 文件上传
* @param session
* @return
*/
@RequestMapping(value="/sUpload",method = RequestMethod.POST)
public void sUpload(@RequestParam(value="location",required=false) MultipartFile mulFile,HttpServletRequest request,HttpServletResponse response,HttpSession session) {
StuLogin stuLogin = (StuLogin) session.getAttribute("stuLogin");//获取session,取得stuId,deptId,classId
System.out.println("stuLoginSESSION:"+stuLogin);
System.out.println("character:"+request.getCharacterEncoding());
TeacherInfo tInfo = studentService.queryTeacherInfo(stuLogin);//查询教师信息
Document document=new Document();//文档实体
String docPath = "I:\\SSMDM\\"; //磁盘绝对路径根路径
String absPath;//绝对路径
String newFileName = null;//文档名
String virPath="/doc/";//数据库存储虚拟相对路径
String dirDName = stuLogin.getDeptId();//文件夹命名:院系id
String dirCName = stuLogin.getClassId();//文件夹命名:班级id
String dirYear = "20"+stuLogin.getStuId().substring(0,2)+"级";//截取学号2位,组合为年级,如:2015级
String dirSName =stuLogin.getStuId();//文件命名:学号+文档类型
String docType =request.getParameter("docType");//文档类型
document.setDocId(stuLogin.getStuId()+docType);
document.setStuId(stuLogin.getStuId());
document.setWorkId(tInfo.getWorkId());
document.setDeptId(stuLogin.getDeptId());
document.setClassId(stuLogin.getClassId());
document.setDocType(docType);
System.out.println("document实体"+document);
String originalFileName = null;
originalFileName = mulFile.getOriginalFilename();
System.out.println("oriname"+originalFileName);
boolean isUploadSuccess=false;
PrintWriter out = null;
File newFile = null;
try {
out = response.getWriter();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//查询指定id文档是否已存在
if((studentService.queryDocByDocId(stuLogin.getStuId()+docType))==null) {
// 上传文档
if (mulFile != null && originalFileName != null && originalFileName.length() > 0) {
newFileName = stuLogin.getStuId()+docType+ originalFileName.substring(originalFileName.lastIndexOf("."));//新文件名
absPath = docPath +dirDName+"\\"+dirYear+"\\"+dirCName+"\\"+dirSName+"\\";//绝对路径
newFile = new File(absPath + newFileName);
if(!newFile.exists()){//路径判断
File dir = new File(absPath);
dir.mkdirs();
}
// 将内存中的数据写入磁盘
try {
mulFile.transferTo(newFile);
} catch (IllegalStateException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
virPath+=dirDName+"/"+dirYear+"/"+dirCName+"/"+dirSName+"/"+newFileName;
document.setLocation(virPath);
isUploadSuccess = studentService.sUpload(document);
}
if (isUploadSuccess==true) {// 当保存成功时
out.println("");
//str="{\"info\":\"提交信息成功!\"}";
} else {// 当保存失败时
out.println("");
//str="{\"info\":\"提交信息失败!\"}";
}
}else {
out.println("");
}
System.out.println("数据库表虚拟路径:"+virPath);
System.out.println("文件绝对路径:"+newFile);
}