HttpClient를 이용해 multipart 업로드

이승현·2022년 11월 3일
0

Upload.java


	public static void main(String[] args) throws Exception {
		uploadUsingHttpClient();
	}
	
	// Uploading Text and a Text File Part
	private static void uploadUsingHttpClient() throws ClientProtocolException, IOException {
		
		HttpPost postRequest = new HttpPost("http://localhost:8080/UploadClient");
		
		/*File 설정*/
		File file = new File("C:\\Workspace\\J_CmsBatch\\log\\TEST_INSERT.log");
		FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY);
		StringBody stringBody1 = new StringBody("이승현",ContentType.DEFAULT_TEXT);
		StringBody stringBody2 = new StringBody("테스트 파일",ContentType.DEFAULT_TEXT);
		
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
		builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
		builder.addPart("uploadFile",fileBody);
		builder.addPart("user",stringBody1);
		builder.addPart("fileName",stringBody2);
		
		
		HttpEntity entity = builder.build();
		HttpClient client = HttpClients.createDefault();
		postRequest.setEntity(entity);
		HttpResponse response = client.execute(postRequest);
	}

MultiPartController.java

package com.controller;

import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.apache.http.client.ClientProtocolException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.service.Upload;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
public class MultiPartController {
	
	@Autowired
	Upload upload;
	//파일 , text 2개 업로드
	@RequestMapping(value="/UploadClient", method=RequestMethod.POST)
	public void useMultipart0(@RequestParam(value="uploadFile", required = false) MultipartFile file, HttpServletRequest req) throws ClientProtocolException, IOException, ServletException {
		System.out.println("UploadClient 진입");
		
		Map<String,String> map = new HashMap<>();
		req.setCharacterEncoding("utf-8");
		
		String uploadFolder = "C:\\Workspace\\webTest\\src\\main\\webapp\\resources\\upload";
		
		/** request의 모든 파라미터 저장  */
		Enumeration<String> data = req.getParameterNames();
		
		/** 업로더와 파일명 출력  */
		while(data.hasMoreElements()) {
			
			String name = (String)data.nextElement();
			String value = req.getParameter(name);
			
			//업로드한 사람, 지정한 파일명 txt
			map.put(name, value);
		}
		
		//진짜 파일명 저장
		map.put("uploadFile", file.getOriginalFilename());
		
		//저장할 경로, 파일 이름 new File
		File saveFile = new File(uploadFolder, file.getOriginalFilename());
		file.transferTo(saveFile);
		
		System.out.println(map);
		log.info("Upload File Name :: " + file.getOriginalFilename());
	}
}

0개의 댓글