Spring Boot搭建文件上傳服務(wù)的方法
本文實例為大家分享了Spring Boot搭建文件上傳服務(wù)的具體代碼,供大家參考,具體內(nèi)容如下
一、服務(wù)端
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();
/**
* 設(shè)置最大上傳文件的大小,默認是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調(diào)用
先配置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();
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于java入門與java開發(fā)環(huán)境配置詳細教程
這篇文章主要介紹了關(guān)于java入門與java開發(fā)環(huán)境配置詳細教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
如何解決java.net.BindException:地址已在使用問題
當Zookeeper啟動報錯“java.net.BindException:地址已在使用”時,通常是因為指定的端口已被其他進程占用,解決這個問題需要按照以下步驟操作:首先,使用命令如lsof -i:2181找到占用該端口的進程號;其次,使用kill命令終止該進程2024-09-09
已有的springcloud+mybatis項目升級為mybatis-plus的方法
這篇文章主要介紹了已有的springcloud+mybatis項目升級為mybatis-plus,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
使用socket實現(xiàn)網(wǎng)絡(luò)聊天室和私聊功能
這篇文章主要介紹了使用socket實現(xiàn)網(wǎng)絡(luò)聊天室和私聊功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12

