//보내는부분
private void Fnc(File uploadFile){
try {
			String url = "Http://host/getURL";
			String charset = "UTF-8";
			File binaryFile = uploadFile;
			String boundary = Long.toHexString(System.currentTimeMillis());
			String CRLF = "\r\n";

			URLConnection connection = new URL(url).openConnection();
			connection.setDoOutput(true);
			connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

			OutputStream output = null;
			try {
				output = connection.getOutputStream();
				PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);
				// Send binary file.
				writer.append("--" + boundary).append(CRLF);
				writer.append("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" + binaryFile.getName() + "\"").append(CRLF);
				writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getName())).append(CRLF);
				writer.append("Content-Transfer-Encoding: binary").append(CRLF);
				writer.append(CRLF).flush();
				Files.copy(binaryFile.toPath(), output);
				output.flush(); // Important before continuing with writer!
				writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.

				// End of multipart/form-data.
				writer.append("--" + boundary + "--").append(CRLF).flush();
			} catch (Exception e) {
				e.printStackTrace();
			}
			int responseCode = ((HttpURLConnection) connection).getResponseCode();
			System.out.println(responseCode); // Should be 200

		} catch (Exception e) {
			e.printStackTrace();
		}
}

//받는부분
@RequestMapping(value = "/getURL", method = RequestMethod.POST)
	private void getURL(HttpServletRequest request, HttpServletResponse response) throws Exception {

		
		MultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
		MultipartHttpServletRequest multipartRequest = resolver.resolveMultipart(request);

		File directory = CommonUtil.getImageDirectory(multipartRequest);

		Iterator<String> iterator = multipartRequest.getFileNames();
		int successCount = 0;
		while (iterator.hasNext()) {
			MultipartFile multipartFile = multipartRequest.getFile(iterator.next());
			multipartFile.getSize();

			String fileName = multipartFile.getOriginalFilename();

			File uploadFile = new File(directory, fileName);
			multipartFile.transferTo(uploadFile);
		}
	

	}
    
    
//디렉터리 생성 유틸

		//로컬
		 ServletContext servlet = request.getSession().getServletContext();
		 String root = servlet.getRealPath("/") + UPLOAD_FOLDER + File.separator;

		//서버
		//String root = "/data" + File.separator + UPLOAD_FOLDER + File.separator;

		System.out.println("root = " + root);

		String dayPath = new SimpleDateFormat("yyMMdd").format(new Date());

		dayPath = root + File.separator + dayPath;

		File dayDirectory = new File(dayPath);
		if (!dayDirectory.isDirectory()) {
			dayDirectory.mkdirs();
		}

		int subCount = dayDirectory.listFiles().length;

		if (subCount == 0) {
			String subPath = dayDirectory + File.separator + subCount;
			File subDirectory = new File(subPath);
			subDirectory.mkdirs();
			return subDirectory;
		} else {
			String subPath = dayDirectory + File.separator + (subCount - 1);
			File subDirectory = new File(subPath);
			if (subDirectory.isDirectory()) {
				if (subDirectory.listFiles().length > 0) {
					subPath = dayDirectory + File.separator + subCount;
					subDirectory = new File(subPath);
					subDirectory.mkdirs();
				}
			} else {
				subDirectory.mkdirs();
			}
			return subDirectory;
		}

 

'프로그래밍 > Java,eclipse' 카테고리의 다른 글

httpConnection 으로 데이터 받기 (저장용)  (0) 2021.07.15
이해하기 폴리곤 알고리즘  (0) 2019.05.23
eclipse SVN 설치중 오류  (0) 2019.05.02
배경 테마 바꾸기  (0) 2018.10.25
연산자의 종류  (0) 2018.10.25

+ Recent posts