Spring boot 文件上傳與文件轉發


Spring boot 文件上傳與文件轉發
開發過程中應該離不開文件上傳這一關的吧
   例如:  即時通訊和各種社交網站啊,為了讓人關注到自己,也為了把自己更好的介紹給別人,頭像上傳,財務系統  想到財務就想到了excel   整天和excel打交道的人   excel上傳,批量數據等
那么Spring boot里面怎么來實現文件上傳呢?以及如果只是做文件上傳的中轉站而目標服務器是在其它地方呢?

新引入的包
[XML]  純文本查看 復制代碼
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
< dependency >
             < groupId >org.apache.httpcomponents</ groupId >
             < artifactId >httpmime</ artifactId >
             < version >4.5.10</ version >
         </ dependency >
         < dependency >
             < groupId >org.apache.httpcomponents</ groupId >
             < artifactId >httpclient</ artifactId >
             < version >4.5.10</ version >
         </ dependency >
  
         <!-- [url]https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient[/url] -->
  
         < dependency >
             < groupId >commons-fileupload</ groupId >
             < artifactId >commons-fileupload</ artifactId >
             < version >1.3.1</ version >
         </ dependency >

文件轉發的工具類
[Java]  純文本查看 復制代碼
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.example.demo.util;
  
import java.io.IOException;
import java.nio.charset.Charset;
  
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
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;
import org.springframework.web.multipart.MultipartFile;
  
  
public class HttpClientUtil {
  
     public static String httpClientUploadFile(String url,MultipartFile file) {
//        String remote_url = url;// 第三方服務器請求地址
         CloseableHttpClient httpClient = HttpClients.createDefault();
         String result = "" ;
         try {
             String fileName = file.getOriginalFilename();
             HttpPost httpPost = new HttpPost(url);
             MultipartEntityBuilder builder = MultipartEntityBuilder.create();
             builder.addBinaryBody( "file" , file.getInputStream(), ContentType.MULTIPART_FORM_DATA, fileName); // 文件流
             builder.addTextBody( "type" , "2" ); // 類似瀏覽器表單提交,對應input的name和value
             HttpEntity entity = builder.build();
             httpPost.setEntity(entity);
             HttpResponse response = httpClient.execute(httpPost); // 執行提交
             HttpEntity responseEntity = response.getEntity();
             if (responseEntity != null ) {
                 // 將響應內容轉換為字符串
                 result = EntityUtils.toString(responseEntity, Charset.forName( "UTF-8" ));
             }
         } catch (IOException e) {
             e.printStackTrace();
         } catch (Exception e) {
             e.printStackTrace();
         } finally {
             try {
                 httpClient.close();
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }
         return result;
     }
}

[Java]  純文本查看 復制代碼
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
package com.example.demo.config;
  
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
  
@Configuration
public class ConfigBean {
  
     @Bean
     public CommonsMultipartResolver multipartResolver(){
         CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
         multipartResolver.setDefaultEncoding( "UTF-8" );
         multipartResolver.setMaxUploadSize( 104857600 );
         multipartResolver.setMaxInMemorySize( 40960 );
         return multipartResolver;
     }
}

[Java]  純文本查看 復制代碼
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@RequestMapping (value = "/uploadFile/upload" , method = RequestMethod.POST)
     @ResponseBody
     public String upload( @RequestParam (value = "file" ) MultipartFile file) {
         if (file.isEmpty()) {
             return "上傳失敗,請選擇文件" ;
         }
         String filename = UUID.randomUUID().toString();
         String suffix = "" ;
         String originalFilename = file.getOriginalFilename();
         // 截取文件的后綴名
         if (originalFilename.contains( "." )) {
             suffix = originalFilename.substring(originalFilename.lastIndexOf( "." ));
         }
         String fileName = filename + suffix;
         String filePath = "D:\\A\\B\\D\\" ;
         File dest = new File(filePath + fileName);
         try {
             file.transferTo(dest);
             return "上傳成功" ;
         } catch (IOException e) {
         }
         return "上傳失敗!" ;
     }
  
     @RequestMapping (value = "/uploadFile/uploadPermanent" , method = RequestMethod.POST)
     @ResponseBody
     public String uploadPermanent( @RequestParam (value = "file" ) MultipartFile file) {
         String url = "http://192.168.1.56:9090" ;
         String ss= HttpClientUtil.httpClientUploadFile(url+ "/uploadFile/upload" ,file);
         return ss;
     }



如圖就說明文件上傳已經好了 


這里環繞了兩次,一次是到中轉方法上的,一個是到目標方法上,當然這里是把中轉服務器和目標服務器寫到了一起,但是場景是滿足中轉條件的
當然還有非常多的方法來幫助我們辦到這些


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM