728x90

파이썬 간단한 파일 공유

공유할 폴더로 이동후에

 

python3 -m http.server 8000

 

서버아이피:8000으로 접속하면 파일목록이 보인다.

 

728x90
728x90

Spring 서버 to 서버 파일 전송

 

A 서버에서 B서버로 파일 전송

[보내는 서버]

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public ResponseEntity<?> uploadImages(@RequestPart("images") final MultipartFile[] files) throws IOException {
    LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
    String response;
    HttpStatus httpStatus = HttpStatus.CREATED;

    try {
        for (MultipartFile file : files) {
            if (!file.isEmpty()) {
                map.add("images", new MultipartInputStreamFileResource(file.getInputStream(), file.getOriginalFilename()));
            }
        }

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        String url = "http://example.com/upload";

        HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<>(map, headers);
        response = restTemplate.postForObject(url, requestEntity, String.class);

    } catch (HttpStatusCodeException e) {
        httpStatus = HttpStatus.valueOf(e.getStatusCode().value());
        response = e.getResponseBodyAsString();
    } catch (Exception e) {
        httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
        response = e.getMessage();
    }

    return new ResponseEntity<>(response, httpStatus);
}

class MultipartInputStreamFileResource extends InputStreamResource {

    private final String filename;

    MultipartInputStreamFileResource(InputStream inputStream, String filename) {
        super(inputStream);
        this.filename = filename;
    }

    @Override
    public String getFilename() {
        return this.filename;
    }

    @Override
    public long contentLength() throws IOException {
        return -1; // we do not want to generally read the whole stream into memory ...
    }
}

[받는 서버]

@RequestMapping("/upload")
@ResponseBody
public String upload(List<MultipartFile> files) {
    files.forEach(file -> {
        System.out.println(file.getContentType());
        System.out.println(file.getOriginalFilename());
    });
    HashMap<String, String> resultMap = new HashMap<>();
    resultMap.put("result", "success");
    return ResponseEntity.ok(resultMap);
}

[참고]

https://velog.io/@dailylifecoding/Spring-MVC-multipartFile-sending-technique

 

[Spring MVC] MultipartFile을 서버 또는 Java 코드에서 전송하는 방법

Java 혹은 Spring MVC + Servlet 기반의 서버에서 MultipartForm 을 전송하는 방식에 대해서 알아본다.

velog.io

https://stackoverflow.com/questions/28408271/how-to-send-multipart-form-data-with-resttemplate-spring-mvc

 

How to send Multipart form data with restTemplate Spring-mvc

I am trying to upload a file with RestTemplate to Raspberry Pi with Jetty. On Pi there is a servlet running: protected void doPost(HttpServletReq...

stackoverflow.com

 

728x90

+ Recent posts