栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Boot搭建文件上传服务的方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Boot搭建文件上传服务的方法

本文实例为大家分享了Spring Boot搭建文件上传服务的具体代码,供大家参考,具体内容如下

一、服务端

pom.xml

 
 
 4.0.0 
  
  org.springframework.boot 
  spring-boot-starter-parent 
  1.3.3.RELEASE 
  
 
 com.test.spring 
 spring-boot 
 1.0.0 
 jar 
 
 spring-boot 
 http://maven.apache.org 
 
  
  UTF-8 
  1.8 
  1.8 
  
 
  
   
   org.springframework.boot 
   spring-boot-starter-web 
   
   
   commons-io 
   commons-io 
   2.4 
   
  
 
 
package com.test.spring; 
 
import java.io.FileOutputStream; 
import java.io.InputStream; 
import java.io.OutputStream; 
 
import javax.servlet.MultipartConfigElement; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.Part; 
 
import org.apache.commons.io.IOUtils; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 
import org.springframework.boot.context.embedded.MultipartConfigFactory; 
import org.springframework.context.annotation.Bean; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.ResponseBody; 
 
@Controller 
@EnableAutoConfiguration 
public class FileUploadController 
{ 
 @RequestMapping(value="/upload", method=RequestMethod.POST) 
 @ResponseBody 
 public String upload(HttpServletRequest request) throws Exception 
 { 
  Part part = request.getPart("uploadfile"); 
   
  InputStream input = part.getInputStream(); 
   
  OutputStream output = new FileOutputStream("d:/tmp/" + part.getSubmittedFileName()); 
  IOUtils.copy(input, output); 
   
  output.close(); 
  input.close(); 
   
  return "OK"; 
 } 
 
 @Bean 
 MultipartConfigElement createMultipartConfigElement() 
 { 
  MultipartConfigFactory mcf = new MultipartConfigFactory(); 
   
  mcf.setMaxFileSize("50MB"); 
  return mcf.createMultipartConfig(); 
 } 
  
 public static void main(String[] args) throws Exception { 
  SpringApplication.run(FileUploadController.class, args); 
 } 
} 

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

二、客户端

客户端使用httpclient调用

先配置maven依赖

 
 org.apache.httpcomponents 
 httpclient 
 4.5.2 
 
 
 org.apache.httpcomponents 
 httpmime 
 4.5.2 
 

测试代码

package com.test.upload; 
 
import java.io.File; 
 
import org.apache.http.HttpEntity; 
import org.apache.http.client.methods.CloseableHttpResponse; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.mime.MultipartEntityBuilder; 
import org.apache.http.impl.client.CloseableHttpClient; 
import org.apache.http.impl.client.HttpClients; 
import org.apache.http.util.EntityUtils; 
 
public class HttpUpload 
{ 
 public static void main(String[] args)throws Exception 
 { 
  String url = "http://127.0.0.1:8080/upload"; 
  CloseableHttpClient client = HttpClients.createDefault(); 
   
  HttpPost httppost = new HttpPost(url); 
   
  MultipartEntityBuilder builder = MultipartEntityBuilder.create(); 
  builder.addBinaryBody("uploadfile", new File("D:/develop/apache-karaf-3.0.4.zip")); 
   
  HttpEntity reqEntity = builder.build(); 
   
  httppost.setEntity(reqEntity); 
   
  CloseableHttpResponse resp = client.execute(httppost); 
   
  String str = EntityUtils.toString(resp.getEntity()); 
  System.out.println(str); 
   
  resp.close(); 
  client.close(); 
 } 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/143150.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号