当前位置: 移动技术网 > IT编程>开发语言>Java > Spring Boot搭建文件上传服务的方法

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

2019年07月19日  | 移动技术网IT编程  | 我要评论

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

一、服务端

pom.xml

<project xmlns="http://maven.apache.org/pom/4.0.0" 
   xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" 
   xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
 
 <modelversion>4.0.0</modelversion> 
 <parent> 
  <groupid>org.springframework.boot</groupid> 
  <artifactid>spring-boot-starter-parent</artifactid> 
  <version>1.3.3.release</version> 
 </parent> 
 
 <groupid>com.test.spring</groupid> 
 <artifactid>spring-boot</artifactid> 
 <version>1.0.0</version> 
 <packaging>jar</packaging> 
 
 <name>spring-boot</name> 
 <url>http://maven.apache.org</url> 
 
 <properties> 
  <project.build.sourceencoding>utf-8</project.build.sourceencoding> 
  <maven.compiler.source>1.8</maven.compiler.source> 
  <maven.compiler.target>1.8</maven.compiler.target> 
 </properties> 
 
 <dependencies> 
  <dependency> 
   <groupid>org.springframework.boot</groupid> 
   <artifactid>spring-boot-starter-web</artifactid> 
  </dependency> 
  <dependency> 
   <groupid>commons-io</groupid> 
   <artifactid>commons-io</artifactid> 
   <version>2.4</version> 
  </dependency> 
 </dependencies> 
 
</project> 
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(); 
  /** 
   * 设置最大上传文件的大小,默认是10mb 
   */ 
  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依赖

<dependency> 
 <groupid>org.apache.httpcomponents</groupid> 
 <artifactid>httpclient</artifactid> 
 <version>4.5.2</version> 
</dependency> 
<dependency> 
 <groupid>org.apache.httpcomponents</groupid> 
 <artifactid>httpmime</artifactid> 
 <version>4.5.2</version> 
</dependency> 

测试代码

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(); 
 } 
} 

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网