봄 부팅 파일 업로드 및 파일 전송

봄 부팅 파일 업로드 및 파일 전송
개발 과정은이 떨어져 업로드 파일에서 분리되어서는 안된다
   , 그 사람에게 관심을 지불하는 인스턴트 메시징 및 소셜 네트워킹 사이트의 다양한 아, 그리고 더 나은 다른 사람에게 자신을 소개 : 등을 아바타 업로드, 금융 시스템은 사람들이 금융 엑셀 엑셀의 모든 일 및 엑셀 업로드 배치 데이터를 다룰 생각 생각
에 업로드 봄 부팅 내부가 어떻게 파일을 달성 할 수 있도록? 그냥 파일 업로드를위한 환승 지점을하고 있다면 대상 서버는 다른 장소에?

새로 도입 된 패키지

[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>


파일 전송 유틸리티 클래스

[자바]  일반 텍스트보기  복사 코드

?

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;

    }

}

 

[자바]  일반 텍스트보기  복사 코드

?

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;

    }

}

 

[자바]  일반 텍스트보기  복사 코드

?

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;

    }




그림은 파일 업로드가 좋았다 표시 


대상에 방법입니다 통과 방법에 한 번, 두 번에 둘러싸여, 여기, 물론, 거기에 교통 및 대상 서버를 함께 기록 될,하지만 장면은 교통의 조건을 충족하는 것입니다
물론 우리가 할이 도움을하는 방법에는 여러 가지가 있습니다

itheimaGZ GET : 더 자바 학습 자료는 염려 할 수있다

게시 된 731 개 원래 기사 · 원의 찬양 3 ·은 110,000 + 조회수

추천

출처blog.csdn.net/u010395024/article/details/104813687