目录
一、上传文件
@PostMapping("/upload")
public String uploadSystem(MultipartFile file){
String url = "";
try{
String name = file.getOriginalFilename().substring(0,file.getOriginalFilename().indexOf(".")+ ".".length()-1);
String imgName =name+"_"+ DateUtils.dateTimeNow();
if (file == null) {
log.error("==> 没有上传文件。");
return ("没有上传文件。");
}
log.info("==>文件名: " + file.getOriginalFilename());
url = fileService.handlerMultipartFile(file,imgName);
log.info("==>文件路径: " + url);
}
catch (Exception e){
}
return url;
}
二、下载文件
/**
* 下载通用配置指导书
* @param
* @param response
* @param filePath 文件路径
*/
public void downloadInstruction(HttpServletResponse response, String filePath) {
// File file = new File(filePath);
File file = new File(filePath);
String fileName = file.getName();
InputStream fis = null;
try {
fis = new FileInputStream(file);
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/PDF;charset=utf-8");
response.setHeader("content-disposition", "attachement;fileName=" + new String(fileName.getBytes("utf-8"), "ISO-8859-1"));
byte[] bytes = new byte[1024];
int len;
while ((len = fis.read(bytes)) != -1) {
response.getOutputStream().write(bytes, 0, len);
}
response.flushBuffer();
fis.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
// return "下载成功";
}
三、预览文件
public void preview( HttpServletResponse response,String filePath) throws
IOException {
try {
File file = new File(filePath);
String pdfName = file.getName();
response.reset();
response.setContentType("application/pdf;charset=UTF-8");
response.setHeader("Content-Disposition", "inline;filename=" + URLEncoder.encode(pdfName, "UTF-8"));//这里的名字并不起作用
OutputStream out = response.getOutputStream();
PDDocument document = PDDocument.load(new FileInputStream(file)); //加载pdf
PDDocumentInformation info = document.getDocumentInformation(); //获得文档属性对象
info.setTitle(pdfName); //修改标题属性 这个标题会被展示
document.setDocumentInformation(info);
document.save(out); //修改完直接输出到响应体中
document.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
FileService
package com.ruoyi.zw.controller;
/**
* @author: cyf
* @Date: 2023/3/27 19:08
*/
import com.ruoyi.common.config.RuoYiConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
@Service
public class FileService {
protected static final Logger logger= LoggerFactory.getLogger(FileService.class);
private String directoryPath = RuoYiConfig.getFile();
public FileService() {
File directory = new File(directoryPath);
if (!directory.exists()) {
directory.mkdirs();
}
}
public String handlerMultipartFile(MultipartFile multipartFile ,String unid) {
String fileOldName = multipartFile.getOriginalFilename();
int beginIndex = fileOldName.lastIndexOf(".");
String suffix = fileOldName.substring(beginIndex);
String newFileName = unid+ suffix;
File upFile = new File(directoryPath + "/" + newFileName);
OutputStream outputStream = null;
try {
byte[] fileByte = multipartFile.getBytes();
outputStream = new FileOutputStream(upFile);
outputStream.write(fileByte);
logger.info("<== 文件写出完成: " + newFileName);
return newFileName;
} catch (Exception e) {
logger.error("", e);
} finally {
try {
if (outputStream != null) {
outputStream.flush();
outputStream.close();
}
} catch (Exception e) {
logger.error("", e);
}
}
return newFileName;
}
}