SpringBoot上传下载

简介: SpringBoot上传下载

额外加入的依赖


<dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.4</version>
    </dependency>


上传


上传页面:注意上传域的name属性要和controller参数名一致,enctype属性


<form method="POST" enctype="multipart/form-data" action="fileUpLoad.action">
    <p>
      文件:<input type="file" name="file" />
    </p>
    <p>
      <input type="submit" value="上传" />
    </p>
  </form>


上传参数控制


package com.example.demo.config;
import javax.servlet.MultipartConfigElement;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * 配置上传
 * @author 柴火
 *
 */
@Configuration
public class FileUpLoadConfig {
  @Bean
  public MultipartConfigElement multipartConfigElement() {
    MultipartConfigFactory factory = new MultipartConfigFactory(); 
     设置文件大小限制,超了,页面会抛出异常信息,这时候就需要进行异常信息的处理了;
    factory.setMaxFileSize("128KB"); // KB,MB 
    /// 设置总上传数据总大小
    factory.setMaxRequestSize("256KB"); 
    // Sets the directory location where files will be stored. 设置上传路径
    //factory.setLocation("路径地址");
    return factory.createMultipartConfig();
  }
}


controller代码(2种上传方式)


package com.example.demo.controller;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class UpLoadController {
  @RequestMapping("/fileUpLoad.action")
  @ResponseBody
  public String upLoadFile(MultipartFile file) throws IllegalStateException, IOException {
    BufferedOutputStream stream = null;
    if (!file.isEmpty()) {
      String path = "E:/stsWorkSpace/SpringBoot_Test/src/main/resources/upload";
      File newFile=new File(path+"/"+file.getOriginalFilename());
      if(!newFile.exists()){
        newFile.createNewFile();
      }
      BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(newFile)); 
      out.write(file.getBytes()); 
      out.flush(); 
      out.close(); 
      return "上传成功";
    }
    return "上传失败";
  }
}


package com.example.demo.controller;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class UpLoadController {
  @RequestMapping("/fileUpLoad.action")
  @ResponseBody
  public String upLoadFile(MultipartFile file) throws IllegalStateException, IOException {
    BufferedOutputStream stream = null;
    if (!file.isEmpty()) {
      // 打印文件的名称
      System.out.println("FileName:" + file.getOriginalFilename());
      // 确定上传文件的位置
      String path = "E:/stsWorkSpace/SpringBoot_Test/src/main/resources/upload";
      File newFile = new File(path, file.getOriginalFilename());
      //如果不存在,创建一哥副本
      if (!newFile.exists()) {
        newFile.createNewFile();
      }
      //将io上传到副本中
      file.transferTo(newFile);
      return "上传成功";
    }
    return "上传失败";
  }
}


下载


package com.example.demo.controller;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class DownLoadController {
  @RequestMapping("/fileDownLoad.action")
  public ResponseEntity<byte[]> textFileDownload() throws Exception {
    String path = "E:/WorkSpace1/SpringMVC_001/WebContent/WEB-INF/fileupload/1.jpg";
    File file = new File(path);
    HttpHeaders headers = new HttpHeaders();
    // String fileName=new
    // String("你好.xlsx".getBytes("UTF-8"),"iso-8859-1");//为了解决中文名称乱码问题
    String fileName = new String(file.getName().getBytes("UTF-8"), "iso-8859-1");// 为了解决中文名称乱码问题
    headers.setContentDispositionFormData("attachment", fileName);
    headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
  }
}
目录
相关文章
|
1天前
|
Java 数据库连接 mybatis
springboot访问jsp页面变成直接下载?
springboot访问jsp页面变成直接下载?
78 0
|
1天前
|
前端开发 Java
SpringBoot下载xlsx模板,导出excel数据
SpringBoot下载xlsx模板,导出excel数据
86 0
|
6月前
|
Java
springboot使用文件流下载
springboot使用文件流下载
|
6月前
|
存储 Java Maven
如何使用Spring Boot和MinIO实现文件上传、读取、下载和删除的功能?
如何使用Spring Boot和MinIO实现文件上传、读取、下载和删除的功能?
537 5
|
1天前
|
存储 Java 对象存储
springboot配置阿里云OSS存储实现文件上传下载功能
【1月更文挑战第1天】springboot配置阿里云OSS存储实现文件上传下载功能
709 2
|
1天前
|
前端开发 Java Apache
Spring Boot文件上传与下载讲解与实战(超详细 附源码)
Spring Boot文件上传与下载讲解与实战(超详细 附源码)
480 0
|
6月前
|
Java
SpringBoot实现文件的上传下载
SpringBoot实现文件的上传下载
|
1天前
|
存储 Java 大数据
Springboot整合Minio实现文件上传和下载
Minio是一个灵活、高性能、开源的对象存储解决方案,适用于各种存储需求,并可以与云计算、容器化、大数据和应用程序集成。它为用户提供了自主控制和可扩展性,使其成为一个强大的存储解决方案。
248 0
|
1天前
|
JavaScript 小程序 Java
基于Java+SpringBoot+Vue的摄影素材分享网站的设计与实现(亮点:活动报名、点赞评论、图片下载、视频下载、在线观看)
基于Java+SpringBoot+Vue的摄影素材分享网站的设计与实现(亮点:活动报名、点赞评论、图片下载、视频下载、在线观看)
60 0
|
10月前
|
前端开发 Java
【Java项目】SpringBoot项目的多文件兼多线程上传下载
【Java项目】SpringBoot项目的多文件兼多线程上传下载
251 0