代码:
1:controller
package com.peraglobal.business.file.controller;
import com.peraglobal.business.file.domain.vo.FileVO;
import com.peraglobal.business.file.service.FileService;
import com.peraglobal.common.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Api(tags = "文件服务")
@RestController
@RequestMapping("/file")
public class FileController {
@Resource
private FileService fileService;
@ApiOperation("文件上传")
@PostMapping("/upload")
public R<FileVO> uploadFile(@ApiParam("文件流") MultipartFile file,
HttpServletRequest request) throws IOException {
return fileService.upload(file, request);
}
@ApiOperation("文件下载")
@PostMapping("/download")
public void downloadFile(HttpServletRequest request,
HttpServletResponse response) throws IOException {
fileService.downloadFile(request, response);
}
}
2:service
package com.peraglobal.business.file.service;
import com.peraglobal.business.file.domain.vo.FileVO;
import com.peraglobal.common.util.R;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public interface FileService {
/**
* 文件上传
* @param file
* @param request
* @return
*/
R<FileVO> upload(MultipartFile file, HttpServletRequest request) throws IOException;
/**
* 文件下载
* @param request
* @return
*/
void downloadFile(HttpServletRequest request, HttpServletResponse response) throws IOException;
}
3:impl
package com.peraglobal.business.file.service.impl;
import com.peraglobal.business.file.domain.vo.FileVO;
import com.peraglobal.business.file.service.FileService;
import com.peraglobal.common.util.ChainUtil;
import com.peraglobal.common.util.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHeaders;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
@Slf4j
@Service
public class FileServiceImpl implements FileService {
private static String localPath = "D:/file";
private static final int BUFFER_SIZE = 10*1024;
@Override
public R<FileVO> upload(MultipartFile file, HttpServletRequest request) throws IOException {
FileVO fileVO = new FileVO();
if(file == null){
return R.failed("文件为空");
}
FileOutputStream fileOutputStream = null;
String dateId = ChainUtil.getDateId();
String originalFilename = file.getOriginalFilename();
String s = dateId +"_"+ originalFilename;
String filePath = localPath+"/"+ s;
File upFile = new File(filePath);
if( ! upFile.exists()){
upFile.createNewFile();
}
byte[] bytes = file.getBytes();
try {
fileOutputStream = new FileOutputStream(upFile);
fileOutputStream.write(bytes);
}catch (Exception e){
log.error("文件上传失败", e);
}finally {
if(fileOutputStream != null){
fileOutputStream.flush();
fileOutputStream.close();
}
}
fileVO.setFileName(s);
fileVO.setFilePath("/toolchain/file/"+s);
return R.ok(fileVO);
}
@Override
public void downloadFile(HttpServletRequest request, HttpServletResponse response) throws IOException {
String fileName = "20231211173212linux命令.txt";
String filePath = localPath+"/"+ fileName;
File file = new File(filePath);
if(!file.exists()){
}
InputStream inputStream = new BufferedInputStream(new FileInputStream(filePath));
response.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
String fileName2 = URLEncoder.encode(fileName);
response.setHeader("Content-Disposition", "attachment;filename=" + fileName2 + ";" + "filename*=utf-8''" + fileName2);
ReadableByteChannel src = null;
WritableByteChannel dest = null;
ByteBuffer buffer2 = ByteBuffer.allocate(BUFFER_SIZE);
try {
src = Channels.newChannel(inputStream);
dest = Channels.newChannel(response.getOutputStream());
while ((src.read(buffer2)) > 0) {
buffer2.flip();
dest.write(buffer2);
buffer2.compact();
}
src.close();
dest.close();
} catch (IOException e) {
e.printStackTrace();
log.error(e.getMessage());
}finally {
closeChanel(src);
closeChanel(dest);
}
}
/**
* 关闭流
* @param channel
*/
private static void closeChanel(Channel channel){
if(channel != null){
try {
channel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
4:返回VO
package com.peraglobal.business.file.domain.vo;
import lombok.Data;
@Data
public class FileVO {
private String fileName;
private String filePath;
}
5:说明
R为我自己定义的返回对象,每个人的返回不同根据自己需求定义,可以返回JSONObject或者String,代码就不放上去了
调用
1:上传
可以看到上传成功。
2:下载
根据上传的文件名下载,后台写死了,自己根据需求修改业务逻辑。