Jojo的作业系统

编程入门 行业动态 更新时间:2024-10-22 11:00:09

Jojo的<a href=https://www.elefans.com/category/jswz/34/1771149.html style=作业系统"/>

Jojo的作业系统

简单的文件上传

代码如下

@RestController
@RequestMapping("/file")
public class FileController {@RequestMapping(method = RequestMethod.POST, value ="/upload")@ResponseBodypublic String uploadFile(@RequestParam("fileName") MultipartFile file) throws IOException {System.out.println("file name = "+file.getOriginalFilename());if (file.isEmpty()) {return "file is empty";}// 获取文件名String fileName = file.getOriginalFilename();// 获取后缀String suffixName = fileName.substring(fileName.lastIndexOf("."));// 文件上产的路径String filePath = "d:\\macro";// fileName处理
//        fileName = filePath+ UUID.randomUUID()+fileName;fileName = filePath+ Path.SEPARATOR+fileName;// 文件对象File dest = new File(fileName);// 创建路径if(!dest.getParentFile().exists()){dest.getParentFile().mkdir();}try {file.transferTo(dest);return "上传成功";} catch (IOException e) {e.printStackTrace();throw e;}}@RequestMapping("download")public void download(HttpServletResponse response) throws FileNotFoundException {File file =new File("C:\\Users\\ASUS\\Desktop\\spring-boot-reference.pdf");FileInputStream fileInputStream=new FileInputStream(file);// 设置被下载而不是被打开response.setContentType("application/gorce-download");// 设置被第三方工具打开,设置下载的文件名response.addHeader("Content-disposition","attachment;fileName=spring-boot-reference.pdf");try {OutputStream outputStream = response.getOutputStream();byte[] bytes = new byte[1024];int len = 0;while ((len = fileInputStream.read(bytes))!=-1){outputStream.write(bytes,0,len);}} catch (IOException e) {e.printStackTrace();}}
}

Postman的设置,注意不要添加任何header

引入注册登录系统

更多推荐

Jojo的作业系统

本文发布于:2024-02-06 22:52:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1751442.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:作业   系统   Jojo

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!