728x90
test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>미지의 세계</h1>
<form>
<input type="file" name="sujiFile" value="" onchange="fChg(this)">
</form>
<div id="disp"></div>
<script>
const myDisp = document.querySelector("#disp");
const myForm = document.forms[0];
function fChg(pThis){
let formData = new FormData(myForm);
//아작스로 파일보내기를 하려면 꼭 FormData를 써야함!!
/*
let formData = new FormData(); //무조건 자동으로 multipart/form-data로 전송됨
console.log("로그",pThis.files[0]);
formData.append("sujiFile",pThis.files[0]);
*/
let xhr = new XMLHttpRequest();
xhr.open("post","/merong/mFile",true)
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
console.log(xhr.responseText);
let myImg = document.createElement("img"); //이미지 태그 맹글깅
myImg.src = xhr.responseText; //이미지 경로 세팅
myImg.width = 100;
myImg.height = 100;
myDisp.appendChild(myImg);
}
}
//get방식이외에는 보내는 데이타를 send()안에 매게변수로 보내야함
xhr.send(formData); //꼬옥 문자열로
}
</script>
</body>
</html>
servlet-context.xml 에 이거 달아줌
<!-- 요걸 등록해야 실제 파일업로드를 사용할 수 있음 -->
<beans:bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver"></beans:bean>
servlet-context.xml코드
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<resources mapping="/myfiles/**" location="file:d:/uploads/" />
<!-- 웹결로 물리적경로 -->
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!-- 요걸 등록해야 실제 파일업로드를 사용할 수 있음 -->
<beans:bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver"></beans:bean>
<context:component-scan base-package="com.minu.merong" />
</beans:beans>
web.xml
<!-- 파일업로드 설정 -->
<multipart-config>
<location>d:/temp</location>
<max-file-size>209715200</max-file-size>
<max-request-size>209815200</max-request-size>
<file-size-threshold>0</file-size-threshold>
</multipart-config>
D:드라이브에 uploads파일 생성해야함
Controller
package com.minu.merong.controller;
import java.io.File;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Controller
public class FileController {
@PostMapping(value="/mFile" , produces = "application/json;charset=utf-8")
@ResponseBody //AJAX요청
public String getFile(MultipartFile sujiFile ) throws Exception {
log.debug(sujiFile.getOriginalFilename());
log.debug(""+sujiFile.getSize());
String chahyun = "d:/uploads/" + sujiFile.getOriginalFilename();
//파일 저장하는것은 transferTo를 쓴다.
sujiFile.transferTo(new File(chahyun));
return "OK";
}
}
package com.minu.merong.controller;
import java.io.File;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.minu.merong.vo.Chahyun;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Controller
public class FileController {
@PostMapping(value="/mFile" , produces = "application/json;charset=utf-8")
@ResponseBody //AJAX요청
public String getFile(Chahyun chahyen ) throws Exception {
log.debug(chahyen.getSujiFile().getOriginalFilename());
log.debug(""+chahyen.getSujiFile().getSize());
log.debug(chahyen.getGoodWords());
log.debug(chahyen.getBadWords());
String destPath = "d:/uploads/" + chahyen.getSujiFile().getOriginalFilename();
//파일 저장하는것은 transferTo를 쓴다.
chahyen.getSujiFile().transferTo(new File(destPath));
//물리적 경로에 대응하는 웹경로를 리턴
return "/merong/myfiles/"+chahyen.getSujiFile().getOriginalFilename();
}
/*
@PostMapping(value="/mFile" , produces = "application/json;charset=utf-8")
@ResponseBody //AJAX요청
public String getFile(MultipartFile sujiFile, MultipartHttpServletRequest msr ) throws Exception {
log.debug(sujiFile.getOriginalFilename());
log.debug(""+sujiFile.getSize());
log.debug(msr.getParameter("goodWords"));
log.debug(msr.getParameter("badWords"));
String chahyun = "d:/uploads/" + sujiFile.getOriginalFilename();
//파일 저장하는것은 transferTo를 쓴다.
sujiFile.transferTo(new File(chahyun));
//물리적 경로에 대응하는 웹경로를 리턴
return "/merong/myfiles/"+sujiFile.getOriginalFilename();
}
*/
}
'Spring > Spring 기초' 카테고리의 다른 글
Spring Ajax Post방식 사용 방법 (0) | 2023.08.04 |
---|---|
파일 업로드 서비스impl코드 (0) | 2023.08.02 |
보충수업 Boomerang(부메랑) (0) | 2023.07.28 |
Spring 정리 (0) | 2023.07.27 |
Spring-타일즈(Tiles).hwp 사용방법 (0) | 2023.07.27 |