使用Spring Boot搭建文件上传服务

一:服务端

pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>1.3.3.RELEASE</version>
  9. </parent>
  10. <groupId>com.test.spring</groupId>
  11. <artifactId>spring-boot</artifactId>
  12. <version>1.0.0</version>
  13. <packaging>jar</packaging>
  14. <name>spring-boot</name>
  15. <url>http://maven.apache.org</url>
  16. <properties>
  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18. <maven.compiler.source>1.8</maven.compiler.source>
  19. <maven.compiler.target>1.8</maven.compiler.target>
  20. </properties>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-web</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>commons-io</groupId>
  28. <artifactId>commons-io</artifactId>
  29. <version>2.4</version>
  30. </dependency>
  31. </dependencies>
  32. </project>

  1. package com.test.spring;
  2. import java.io.FileOutputStream;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. import javax.servlet.MultipartConfigElement;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.Part;
  8. import org.apache.commons.io.IOUtils;
  9. import org.springframework.boot.SpringApplication;
  10. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  11. import org.springframework.boot.context.embedded.MultipartConfigFactory;
  12. import org.springframework.context.annotation.Bean;
  13. import org.springframework.stereotype.Controller;
  14. import org.springframework.web.bind.annotation.RequestMapping;
  15. import org.springframework.web.bind.annotation.RequestMethod;
  16. import org.springframework.web.bind.annotation.ResponseBody;
  17. @Controller
  18. @EnableAutoConfiguration
  19. public class FileUploadController
  20. {
  21. @RequestMapping(value="/upload", method=RequestMethod.POST)
  22. @ResponseBody
  23. public String upload(HttpServletRequest request) throws Exception
  24. {
  25. Part part = request.getPart("uploadfile");
  26. InputStream input = part.getInputStream();
  27. OutputStream output = new FileOutputStream("d:/tmp/" + part.getSubmittedFileName());
  28. IOUtils.copy(input, output);
  29. output.close();
  30. input.close();
  31. return "OK";
  32. }
  33. @Bean
  34. MultipartConfigElement createMultipartConfigElement()
  35. {
  36. MultipartConfigFactory mcf = new MultipartConfigFactory();
  37. /**
  38. * 设置最大上传文件的大小,默认是10MB
  39. */
  40. mcf.setMaxFileSize("50MB");
  41. return mcf.createMultipartConfig();
  42. }
  43. public static void main(String[] args) throws Exception {
  44. SpringApplication.run(FileUploadController.class, args);
  45. }
  46. }

注意:spring-boot-starter-web 1.3.3.RELEASE 依赖的servlet是3.1


二:客户端

客户端使用httpclient调用

先配置maven依赖

  1. <dependency>
  2. <groupId>org.apache.httpcomponents</groupId>
  3. <artifactId>httpclient</artifactId>
  4. <version>4.5.2</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.httpcomponents</groupId>
  8. <artifactId>httpmime</artifactId>
  9. <version>4.5.2</version>
  10. </dependency>

测试代码

  1. package com.test.upload;
  2. import java.io.File;
  3. import org.apache.http.HttpEntity;
  4. import org.apache.http.client.methods.CloseableHttpResponse;
  5. import org.apache.http.client.methods.HttpPost;
  6. import org.apache.http.entity.mime.MultipartEntityBuilder;
  7. import org.apache.http.impl.client.CloseableHttpClient;
  8. import org.apache.http.impl.client.HttpClients;
  9. import org.apache.http.util.EntityUtils;
  10. public class HttpUpload
  11. {
  12. public static void main(String[] args)throws Exception
  13. {
  14. String url = "http://127.0.0.1:8080/upload";
  15. CloseableHttpClient client = HttpClients.createDefault();
  16. HttpPost httppost = new HttpPost(url);
  17. MultipartEntityBuilder builder = MultipartEntityBuilder.create();
  18. builder.addBinaryBody("uploadfile", new File("D:/develop/apache-karaf-3.0.4.zip"));
  19. HttpEntity reqEntity = builder.build();
  20. httppost.setEntity(reqEntity);
  21. CloseableHttpResponse resp = client.execute(httppost);
  22. String str = EntityUtils.toString(resp.getEntity());
  23. System.out.println(str);
  24. resp.close();
  25. client.close();
  26. }
  27. }