springboot下载与上传

下载

@RequestMapping("/download")
public void download(HttpServletResponse response){
   response.setCharacterEncoding("UTF-8");
   try {
       InputStream is=new BufferedInputStream(new FileInputStream("./userdata/1.txt"));
       byte[] bytes=new  byte[1024];
       int l=0;
       //设置表头参数
       response.addHeader("Content-Disposition", "inline;filename=1.txt");
       response.addHeader("Content-Length", "" + is.available());
       response.setContentType("application/octet-stream");
       //响应输出流输出
       OutputStream os=new BufferedOutputStream(response.getOutputStream());
       while((l=is.read(bytes))!=-1){
           os.write(bytes);
       }
       is.close();
       os.close();
   }catch (IOException e) {
       e.printStackTrace();
   }
}

上传

  • yml配置
spring:
  servlet:
    multipart:
      max-request-size: 5GB  # 上传文件总的最大值 默认10MB
      max-file-size: 1GB #单个文件最大值 默认10MB

代码

@RequestMapping("/upload")
@ResponseBody
public String upload(@RequestParam MultipartFile multipartFile){
    if(multipartFile==null)return "空";
    String filename=multipartFile.getOriginalFilename();
    String filepath=System.getProperty("user.dir")+"/userdata/";
    File file=new File(filepath);
    if(!file.exists()){
        file.mkdir();
    }
    File newfile=new File(filepath+filename);
    try {
    	//保存到本地
		//multipartFile.transferTo(newfile);

		//文件输出流输出到本地
        FileOutputStream os=new FileOutputStream(newfile);
        os.write(multipartFile.getBytes());
        os.close();
    }catch (IOException e){
        e.printStackTrace();
        return "失败";
    }
    return "成功";
}
  • Content-Disposition

下载文件的一个标识字段,Content-Disposition属性有以下两种类型

inline (默认值):将文件内容直接显示在页面;
attachment:弹出对话框让用户下载。
  • Content-type

Http的实体首部字段,在request的请求行(或response的状态码)之后,也是首部的一部分。用于说明请求或返回的消息主体是用何种方式编码,在request header和response header里都存在。
常见的媒体格式类型如下:

text/html : HTML格式
text/plain :纯文本格式
text/xml : XML格式
image/gif :gif图片格式
image/jpeg :jpg图片格式
image/png:png图片格式

以application开头的媒体格式类型:

application/xhtml+xml :XHTML格式
application/xml : XML数据格式
application/atom+xml :Atom XML聚合格式
application/json : JSON数据格式
application/pdf :pdf格式
application/msword : Word文档格式
application/octet-stream : 二进制流数据(如常见的文件下载)
application/x-www-form-urlencoded : 中默认的encType,form表单数据被编码为key/value格式发送到服务器(表单默认的提交数据的格式)

上传文件使用:

multipart/form-data : 需要在表单中进行文件上传时,就需要使用该格式
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!关于Spring Boot和Vue文件上传下载的问题,我可以为你提供一些解答。 在Spring Boot中,你可以使用Spring MVC的文件上传功能来处理文件上传。首先,你需要在Spring Boot项目中配置一个文件上传的Bean。可以使用`MultipartResolver`接口来实现文件上传功能。下面是一个示例代码: ```java @Configuration public class WebConfig implements WebMvcConfigurer { @Bean public MultipartResolver multipartResolver() { return new StandardServletMultipartResolver(); } } ``` 接下来,你可以创建一个Controller来处理文件上传请求。你可以使用`@RequestParam`注解来接收上传的文件。以下是一个简单的上传文件的示例: ```java @RestController public class FileController { @PostMapping("/upload") public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) { // 处理文件上传逻辑 // 返回上传成功的消息 return ResponseEntity.ok("文件上传成功!"); } } ``` 在Vue中,你可以使用`axios`库来发送文件上传请求。以下是一个简单的文件上传的示例代码: ```javascript <template> <div> <input type="file" ref="fileInput" @change="uploadFile"> </div> </template> <script> import axios from 'axios'; export default { methods: { uploadFile() { const file = this.$refs.fileInput.files[0]; const formData = new FormData(); formData.append('file', file); axios.post('/upload', formData) .then(response => { // 处理上传成功的逻辑 console.log(response.data); }) .catch(error => { // 处理上传失败的逻辑 console.error(error); }); } } } </script> ``` 至于文件下载,你可以在Spring Boot中创建一个Controller来处理下载请求。你可以使用`ResponseEntity`来返回文件内容和正确的HTTP头。以下是一个简单的文件下载的示例: ```java @RestController public class FileController { @GetMapping("/download") public ResponseEntity<Resource> downloadFile() throws IOException { // 加载文件资源 Resource fileResource = new ClassPathResource("path/to/file.pdf"); // 设置HTTP头 HttpHeaders headers = new HttpHeaders(); headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=file.pdf"); return ResponseEntity.ok() .headers(headers) .body(fileResource); } } ``` 在Vue中,你可以使用`window.open`方法来下载文件。以下是一个简单的文件下载的示例代码: ```javascript <template> <div> <button @click="downloadFile">下载文件</button> </div> </template> <script> export default { methods: { downloadFile() { const downloadURL = '/download'; window.open(downloadURL, '_blank'); } } } </script> ``` 希望以上信息能够帮助到你!如果有任何问题,请随时问我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值