(아파치에서 제공해주는 오픈 소스)
https://commons.apache.org/proper/commons-fileupload/ <-- 참조
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
<!-- file upload -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"/>
<property name="maxUploadSize" value="2000000"/> <!-- 2MB -->
</bean>
package controller;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;
@Controller
public class filecontroller {
// Apache Common Fileupload
@RequestMapping("/form.do")
public String form( HttpServletRequest request, HttpServletResponse response, Model model ) {
return "form";
}
@RequestMapping("/form_ok.do")
public String form_ok(@RequestParam("upload") MultipartFile multipartFile) {
String uploadPath="C:/Java/spring-workspace/file2/src/main/webapp/upload";
System.out.println("파일명 : " + multipartFile.getName() );
System.out.println("파일명 : " + multipartFile.getOriginalFilename() );
System.out.println("사이즈 : " + multipartFile.getSize() );
return "form_ok";
}
}
이렇게 줄여서도 사용 가능(업로드는 되지 않음)
@RequestMapping("/form_ok2.do")
public String form_ok2(MultipartFile upload) {
System.out.println("파일명 : " + upload.getName() );
System.out.println("파일명 : " + upload.getOriginalFilename() );
System.out.println("사이즈 : " + upload.getSize() );
return "form_ok";
}
cos 방법과 달리 임시 공간에 저장됨, 따라서 upload폴더로 가져오기 위한 작업 필요
FileOutputStream fos = null;
try {
fos = new FileOutputStream(uploadPath + "/" + multipartFile.getOriginalFilename());
fos.write(multipartFile.getBytes());
} catch (FileNotFoundException e) {
System.out.println("에러 : " + e.getMessage());
} catch (IOException e) {
System.out.println("에러 : " + e.getMessage());
} finally {
if(fos != null) try { fos.close(); } catch(IOException e) {}
}
이 방법을 사용할 경우 DefaultFileRenamePolicy 직접 만들어 줘야 한다.
별도의 request가 없어도 출력이 된다.
@RequestMapping("/form_ok2.do")
public String form_ok2(MultipartFile upload, String data) {
System.out.println("파일명 : " + upload.getName() );
System.out.println("파일명 : " + upload.getOriginalFilename() );
System.out.println("사이즈 : " + upload.getSize() );
System.out.println("데이터 : " + data );
return "form_ok";
}
(view에 댓글창 추가, 쓰기에 위도, 경도 부분 추가)
테이블 구성
create table album1 (
seq int not null primary key auto_increment,
subject varchar(150) not null,
writer varchar(12) not null,
mail varchar(50),
password varchar(12) not null,
content varchar(2000),
filename varchar(50),
filesize int,
latitude varchar(12),
longitude varchar(12),
cmt int not null,
hit int not null,
wip varchar(15) not null,
wdate datetime not null
);
----------------------------------------------------------------------------------------------
댓글
create table album_comment1 (
seq int not null primary key auto_increment,
pseq int not null,
writer varchar(12) not null,
password varchar(12) not null,
content varchar(2000) not null,
wdate datetime not null
);