关于java文件【下载中文文件名乱码】问题解决方案
程序员文章站
2022-03-19 17:41:40
...
前面一篇文章讲了:基于MAVEN项目——如何上传于下载文件
现在遇见一个问题:上传中文命名的图片,下载显示不了中文名称:
解决效果:
解决步骤:
第一步:
sping-mvc.xml部分代码:
<!-- 上传文件的处理器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置默认编码 解决了上传文件名乱码的问题 -->
<property name="defaultEncoding" value="utf-8"></property>
<!-- 上传图片最大大小5M -->
<property name="maxUploadSize" value="5242440"></property>
</bean>
第二步:
FileController.java中文文件名获取代码:
- 获取浏览器格式
- 根据不同浏览器控制文件名编码格式
String userAgent = request.getHeader("User-Agent");//获取浏览器名(IE/Chome/firefox)
if(userAgent.contains("MSIE")||userAgent.contains("Trident")) {//针对IE或IE为内核的浏览器
fileName=java.net.URLEncoder.encode(fileName,"UTF-8");
}else {
fileName=new String(fileName.getBytes("UTF-8"),"ISO-8859-1");//谷歌控制版本
}
headers.setContentDispositionFormData("attachment", fileName);// 默认下载文件名为原始文件名
其他浏览器控制字符格式:(详细)
String userAgent = request.getHeader("User-Agent");//获取浏览器名(IE/Chome/firefox)
if (userAgent.contains("firefox")) {
fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1"); // firefox浏览器
} else if (userAgent.contains("MSIE")) {
fileName = URLEncoder.encode(fileName, "UTF-8");// IE浏览器
}else if (userAgent.contains("CHROME")) {
fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");// 谷歌
}
headers.setContentDispositionFormData("attachment", fileName);// 默认下载文件名为原始文件名
FileController.java全部代码:(我用的浏览器chome)
package com.cungudafa.spingmvc01.controller;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import com.cungudafa.spingmvc01.bean.FileInfo;
import com.cungudafa.spingmvc01.dao.FileInfoDao;
import com.cungudafa.spingmvc01.dao.impl.FileInfoDaoImpl;
import com.cungudafa.spingmvc01.util.FileUtil;
/**
* 处理文件的类
*
* @author Administrator
*
*/
@Controller
@RequestMapping("/file")
public class FileController {
@RequestMapping("/uploadtest")
public String upload(MultipartFile uploadFile) throws IOException {
System.out.println("uploadFile = " + uploadFile);
// 获得上传字节数
byte[] buf = uploadFile.getBytes();
System.out.println("上传文件大小:" + buf.length);
// 获得上传文件名称
System.out.println("上传文件名称getName = " + uploadFile.getName());
System.out.println("上传文件名称getOriginalFilename = " + uploadFile.getOriginalFilename());
return "index";
}
@RequestMapping("/upload")
public String toUpload() {
return "upload";
}
@RequestMapping("/files")
public String files(HttpSession session) {
FileInfoDao dao = new FileInfoDaoImpl();
try {
List<FileInfo> files = dao.findFiles();// 查找数据库全部内容
session.setAttribute("list", files);// 全部文件显示页面,用list可以调出数据库表
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "files";
}
@RequestMapping("/doUpload")
public String upload(MultipartFile uploadFile, HttpServletRequest request) throws IOException {
System.out.println("uploadFile = " + uploadFile);
// 获得文件
byte[] buf = uploadFile.getBytes();
System.out.println("文件长度" + buf.length);
// 文件名
System.out.println("文件名getName = " + uploadFile.getName());
System.out.println("文件名getOriginalFilename = " + uploadFile.getOriginalFilename());
// 文件名 a.txt
String originalFileName = uploadFile.getOriginalFilename();
// 时间戳
String time = FileUtil.createFileTimestamp();
// 文件url /upload/1231231231231a.txt
String fileUrl = "/upload/" + time + originalFileName;
fileUrl = request.getServletContext().getRealPath(fileUrl);
System.out.println("fileUrl = " + fileUrl);
// 向url地址存储文件
FileUtil.writeFileToUrl(uploadFile, fileUrl);
// 向数据库中保存文件信息
FileInfo fileInfo = new FileInfo();
fileInfo.setFileName(originalFileName);
fileInfo.setFileUrl(fileUrl);
FileInfoDao dao = new FileInfoDaoImpl();
try {
dao.addFileInfo(fileInfo);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "user";
}
@RequestMapping("/download")
public ResponseEntity<byte[]> download(Integer fileId,HttpServletRequest request) {
FileInfoDao dao = new FileInfoDaoImpl();
try {
FileInfo fileInfo = dao.findFileById(fileId);// 按学号查找文件记录类
String fileUrl = fileInfo.getFileUrl();
String fileName = fileInfo.getFileName();
HttpHeaders headers = new HttpHeaders();// http协议
String userAgent = request.getHeader("User-Agent");
if(userAgent.contains("MSIE")||userAgent.contains("Trident")) {
fileName=java.net.URLEncoder.encode(fileName,"UTF-8");
}else {
fileName=new String(fileName.getBytes("UTF-8"),"ISO-8859-1");
}
headers.setContentDispositionFormData("attachment", fileName);// 默认下载文件名为原始文件名
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);// 媒体流下载
ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(new File(fileUrl)),
headers, HttpStatus.CREATED);// 二进制转换为实体类型
return entity;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
上一篇: 网红营销如何跟大咖建立联系
下一篇: 王者荣耀与工作