红旗渠建设集团网站内蒙古网络公司排名
news/
2025/9/23 3:31:33/
文章来源:
红旗渠建设集团网站,内蒙古网络公司排名,南宁网站推广策略,wordpress 上传安装不了目录 1、上传文件1.1、代码1.2、postman测试截图 2、下载resources目录中的模板文件2.1、项目结构2.2、代码2.3、使用场景 3、预览文件3.1、项目结构3.2、代码3.3、使用场景 1、上传文件
1.1、代码
PostMapping(/uploadFile)
public String uploadFile(Multipart… 目录 1、上传文件1.1、代码1.2、postman测试截图 2、下载resources目录中的模板文件2.1、项目结构2.2、代码2.3、使用场景 3、预览文件3.1、项目结构3.2、代码3.3、使用场景 1、上传文件
1.1、代码
PostMapping(/uploadFile)
public String uploadFile(MultipartFile file) {System.out.println(文件名称 file.getOriginalFilename());return 成功;
}PostMapping(/uploadFile2)
public String uploadFile2(RequestParam(file) MultipartFile file
) {System.out.println(文件名称 file.getOriginalFilename());return 成功;
}PostMapping(/uploadFile3)
public String uploadFile3(RequestPart(file) MultipartFile file
) {System.out.println(文件名称 file.getOriginalFilename());return 成功;
}// 发送文件的同时带上参数
PostMapping(/uploadFile4)
public String uploadFile4(RequestPart(file) MultipartFile file, // 可以换成“MultipartFile file”或者“RequestParam(file) MultipartFile file”RequestParam(id) String id
) {System.out.println(文件名称 file.getOriginalFilename());System.out.println(id id);return 成功;
}1.2、postman测试截图 2、下载resources目录中的模板文件
2.1、项目结构
假设resources目录下有一个pdf文件用户数据导入模板.xlsx然后我们来下载该文件 2.2、代码
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.MediaTypeFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;RestController
public class TestController {GetMapping(/downloadLocalFile)public void downloadLocalFile(HttpServletResponse response) throws IOException {String fileName 用户数据导入模板.xlsx;Resource r new ClassPathResource(fileName);try (FileInputStream fileInputStream new FileInputStream(r.getFile());ServletOutputStream outputStream response.getOutputStream();) {response.setContentType(application/force-download);try {fileName new String(fileName.getBytes(utf-8), ISO-8859-1);} catch (UnsupportedEncodingException e) {e.printStackTrace();}response.setHeader(Content-Disposition, attachment;filename fileName);IOUtils.copyLarge(fileInputStream, outputStream);} catch (Exception e) {e.printStackTrace();}}
}2.3、使用场景 3、预览文件
3.1、项目结构
以resources下面的文件为例展示预览文件的代码这是从本地获取文件当然也可以通过其他方式获取文件 3.2、代码
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.MediaTypeFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;RestController
public class TestController {GetMapping(/previewLocalFile)public void previewLocalFile(HttpServletResponse response) throws IOException {String fileName SQL必知必会第5版.pdf;Resource r new ClassPathResource(fileName);try (FileInputStream fileInputStream new FileInputStream(r.getFile());ServletOutputStream outputStream response.getOutputStream();) {// 区别点1将“response.setContentType(application/force-download);”替换成下面内容response.setContentType(MediaTypeFactory.getMediaType(fileName).orElse(MediaType.APPLICATION_OCTET_STREAM).toString());try {fileName new String(fileName.getBytes(utf-8), ISO-8859-1);} catch (UnsupportedEncodingException e) {e.printStackTrace();}// 区别点2预览是“filename”下载是“attachment;filename”response.setHeader(Content-Disposition, filename fileName);IOUtils.copyLarge(fileInputStream, outputStream);} catch (Exception e) {e.printStackTrace();}}
}3.3、使用场景
在网盘软件中预览pdf文件
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/911241.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!