目录
一、上传文件
@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 {
FileInputStream is = new FileInputStream(new File(filePath));
// 清空response
response.reset();
//2、设置文件下载方式
response.setCharacterEncoding("utf-8");
response.setContentType("application/pdf");
OutputStream outputStream = response.getOutputStream();
int count = 0;
byte[] buffer = new byte[1024 * 1024];
while ((count = is.read(buffer)) != -1) {
outputStream.write(buffer, 0, count);
}
outputStream.flush();
}
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;
}
}