這篇文章主要介紹Spring Boot如何搭建文件上傳服務(wù),文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
在榮成等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專(zhuān)注、極致的服務(wù)理念,為客戶(hù)提供網(wǎng)站設(shè)計(jì)、網(wǎng)站制作 網(wǎng)站設(shè)計(jì)制作按需策劃設(shè)計(jì),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站建設(shè),網(wǎng)絡(luò)營(yíng)銷(xiāo)推廣,外貿(mào)網(wǎng)站建設(shè),榮成網(wǎng)站建設(shè)費(fèi)用合理。
本文實(shí)例為大家分享了Spring Boot搭建文件上傳服務(wù)的具體代碼,供大家參考,具體內(nèi)容如下
一、服務(wù)端
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(); /** * 設(shè)置最大上傳文件的大小,默認(rèn)是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 依賴(lài)的servlet是3.1
二、客戶(hù)端
客戶(hù)端使用httpclient調(diào)用
先配置maven依賴(lài)
org.apache.httpcomponents httpclient 4.5.2 org.apache.httpcomponents httpmime 4.5.2
測(cè)試代碼
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(); } }
以上是“Spring Boot如何搭建文件上傳服務(wù)”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!