SpringBoot中如何实现下载上传

  介绍

SpringBoot中如何实现下载上传?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

最近在学习SpringBoot,以下是最近学习整理的实现文件上传下载的Java代码:

1,开发环境:

IDEA15 + Maven + JDK1.8

2,新建一个Maven工程:

 SpringBoot中如何实现下载上传”>,<br/> </p> <p> 3,工程框架<br/> </p> <p> <img src= & lt;项目xmlns=癶ttp://maven.apache.org/POM/4.0.0"xmlns: xsi=癶ttp://www.w3.org/2001/XMLSchema-instance"   ,xsi: schemaLocation=癶ttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"的在;   & lt; modelVersion> 4.0.0   & lt; groupId> SpringWebContent   & lt; artifactId> SpringWebContent   & lt; packaging> war   & lt; version> 1.0 -snapshot   & lt; name> SpringWebContent Maven Webapp   & lt; url> http://maven.apache.org</url>   & lt; parent>   & lt; groupId> org.springframework.boot   & lt; artifactId> spring-boot-starter-parent   & lt; version> 1.4.3.RELEASE   & lt;/parent>   & lt; dependencies>   & lt; dependency>   & lt; groupId> org.springframework.boot   & lt; artifactId> spring-boot-starter-thymeleaf   & lt;/dependency>   & lt; dependency>   & lt; groupId> org.springframework.boot   & lt; artifactId> spring-boot-devtools   & lt; optional> true   & lt;/dependency>   & lt; dependency>   & lt; groupId> junit   & lt; artifactId> junit   & lt; version> 3.8.1   & lt; scope> test   & lt;/dependency>   & lt;/dependencies>   & lt; properties>   & lt; java.version> 1.8 & lt;/java.version>   & lt;/properties>   & lt; build>   & lt; finalName> SpringWebContent   & lt; plugins>   & lt; plugin>   & lt; groupId> org.springframework.boot   & lt; artifactId> spring-boot-maven-plugin   & lt;/plugin>   & lt;/plugins>   & lt;/build>   & lt;/project>

5应用程序。java

进口org.springframework.boot.SpringApplication;   进口org.springframework.boot.autoconfigure.SpringBootApplication;   @SpringBootApplication   公共类应用程序{      公共静态void main (String [] args) {   SpringApplication.run (Application.class, args);   }   }

6, FileController。java

进口org.springframework.stereotype.Controller;   进口org.springframework.ui.Model;   进口org.springframework.web.bind.annotation.RequestMapping;   进口org.springframework.web.bind.annotation.RequestMethod;   进口org.springframework.web.bind.annotation.RequestParam;   进口org.slf4j.Logger;   进口org.slf4j.LoggerFactory;   进口org.springframework.web.bind.annotation.ResponseBody;   进口org.springframework.web.multipart.MultipartFile;   进口org.springframework.web.multipart.MultipartHttpServletRequest;   进口javax.servlet.http.HttpServletRequest;   进口javax.servlet.http.HttpServletResponse;   进口. io . *;   进口并不知道;      @ controller   公开课FileController {   @RequestMapping (“/greeting")   公共字符串问候(@RequestParam (value=https://www.yisu.com/zixun/懊帧?要求=false, defaultValue=笆澜纭?的字符串名称,模型模型){   模型。addAttribute(“名字”,名称);   返回“问候”;   }   私有静态最终日志记录器=LoggerFactory.getLogger (FileController.class);//文件上传相关代码   @RequestMapping (value="上传")   @ResponseBody   公共字符串上传(@RequestParam(“测试”)MultipartFile文件){   如果(file.isEmpty ()) {   返回“文件为空”;   }//获取文件名   字符串文件名=file.getOriginalFilename ();   logger.info(“上传的文件名为:" +文件名);//获取文件的后缀名   字符串suffixName=fileName.substring (fileName.lastIndexOf (“。”);   logger.info(“上传的后缀名为:" + suffixName);//文件上传后的路径   字符串filePath=癊://测试//?//解决中文问题,liunx下中文路径,图片显示问题//文件名=UUID.randomUUID () + suffixName;   文件dest=新文件(filePath +文件名);//检测是否存在目录   如果(! dest.getParentFile () .exists ()) {   .mkdirs dest.getParentFile () ();   }   尝试{   file.transferTo(桌子);   回报”上传成功”;   }捕捉(IllegalStateException e) {   e.printStackTrace ();   }捕捉(IOException e) {   e.printStackTrace ();   }   回报”上传失败”;   }//文件下载相关代码   @RequestMapping(/下载)   公共字符串downloadFile (org.apache.catalina.servlet4preview.http。HttpServletRequest请求,HttpServletResponse响应){   字符串文件名=癋ileUploadTests.java”;   如果(文件名!=null) {//当前是从该工程的web - inf//文件//下获取文件(该目录可以在下面一行代码配置),然后下载到C://用户//下载即本机的默认下载的目录   字符串realPath=request.getServletContext () .getRealPath (   “web - inf////?;   文件文件=新文件(realPath(文件名);   如果(file.exists ()) {   response.setContentType(“应用程序/force-download ");//设置强制下载不打开   response.addHeader(“附加”,   “附件;文件名=" +文件名);//设置文件名   byte[]新字节缓冲区=[1024];   FileInputStream fis=零;   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

SpringBoot中如何实现下载上传