--------------------------------------------

Spring MVC Annotation 기반 파일 업로드, 다운로드 



1. 아래 jar 파일 구성

commons-dbcp-1.4.jar

commons-fileupload-1.2.1.jar

commons-io-1.3.2.jar

commons-logging-1.1.1.jar

commons-pool-1.5.4.jar

ibatis-2.3.4.726.jar

jstl.jar

log4j-1.2.15.jar

ojdbc14.jar

spring.jar

spring-webmvc.jar

standard.jar



2. 기존의 SpringMVC 환경 설정 파일 복사


3. 아래 프로그램 작성


//FileSend.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"

    pageEncoding="EUC-KR"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">

<title>Insert title here</title>

</head>

<body>

<div>


<h1>파일 업로드 테스트(SpringMVC Annotation)</h1>

<form action="upload.file" method="post"

enctype="multipart/form-data">

<input type="hidden" name="mode" value="uploadOk">

파일첨부:<input type="file" name="upload"><br>

파일설명:<input type="text" name="content"><br>

<input type="submit" value="전송">

</form>

</div>

</body>

</html>




//FileReceive.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"

    pageEncoding="EUC-KR"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">

<title>Insert title here</title>

</head>

<body>

<div>

<h1>파일 다운로드 테스트(SpringMVC Annotation)</h1>

파일첨부:<a href="download.file?saveFileName=${saveFileName}&originalFileName=${originalFileName}">${originalFileName}</a><br>

파일설명:${content}<br>

파일종류:${contentType}<br>

</div>

</body>

</html>




//FileCommand.java

package com.test;


import org.springframework.web.multipart.MultipartFile;


public class FileCommand {

private String mode;

private String content, saveFileName, originalFileName;

private long fileSize;

private MultipartFile upload;

public String getMode() {

return mode;

}

public void setMode(String mode) {

this.mode = mode;

}

public String getContent() {

return content;

}

public void setContent(String content) {

this.content = content;

}

public String getSaveFileName() {

return saveFileName;

}

public void setSaveFileName(String saveFileName) {

this.saveFileName = saveFileName;

}

public String getOriginalFileName() {

return originalFileName;

}

public void setOriginalFileName(String originalFileName) {

this.originalFileName = originalFileName;

}

public long getFileSize() {

return fileSize;

}

public void setFileSize(long fileSize) {

this.fileSize = fileSize;

}

public MultipartFile getUpload() {

return upload;

}

public void setUpload(MultipartFile upload) {

this.upload = upload;

}


}






//FileController.java

package com.test;


import java.io.InputStream;


import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;


import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;


import com.spring.common.FileManager;



@Controller("file.fileController")

public class FileController {


@Resource(name="fileManager")

private FileManager fileManager;

@RequestMapping(value="/upload.file", method={RequestMethod.GET, RequestMethod.POST})

public String upload(FileCommand command, HttpServletResponse response, HttpServletRequest request) 

throws Exception {

if (command == null 

|| command.getMode() == null

|| command.getMode().equals("")) {

return "/FileSend.jsp";

}

FileCommand dto = command;

request.setAttribute("content", "업로드된 파일이 없습니다.");

if (!dto.getUpload().isEmpty()) {


HttpSession session = request.getSession();

String root = session.getServletContext().getRealPath("/");

String path = root + "pds\\saveFile";

System.out.println("path:"+path);

dto.setFileSize(dto.getUpload().getSize());

dto.setOriginalFileName(dto.getUpload().getOriginalFilename());

InputStream is = dto.getUpload().getInputStream();

String newFileName = fileManager.doFileUpload(is

, dto.getOriginalFileName()

, path);

dto.setSaveFileName(newFileName);

request.setAttribute("saveFileName", dto.getSaveFileName());

request.setAttribute("originalFileName", dto.getOriginalFileName());

request.setAttribute("content", dto.getContent());


request.setAttribute("fileSize", dto.getFileSize());

request.setAttribute("contentType",dto.getUpload().getContentType());

//데이터베이스에 업로드된 파일 정보 저장 프로세스 추가

}

return "/FileReceive.jsp";

}


@RequestMapping(value="/download.file", method={RequestMethod.GET, RequestMethod.POST})

public String download(FileCommand command, HttpServletResponse response, HttpServletRequest request) {

HttpSession session = request.getSession();

String root = session.getServletContext().getRealPath("/");

String path = root + "pds\\saveFile";

FileCommand dto = command;

boolean flag = fileManager.doFileDownload(dto.getSaveFileName()

, dto.getOriginalFileName()

, path

, response);

if (flag) {

//다운로드 횟수 증가 프로세스 추가

}

return "";

}


}




//브라우저에서 아래 주소로 연결

http://localhost:8090/SpringMVC_FildUpload/upload.file

-> 처음에는 FileSend.jsp 페이지가 나온다.

-> 업로드하고자 하는 파일을 선택하고 전송 버튼을 클릭한다.

-> 업로드가 되면 FileReceive.jsp 페이지로 변하게 되고, 업로드된 파일의 정보가 출력된다.

-> 업로드된 파일을 클릭하면 다운로드 가능하다.

-> 웹서버에서 업로드된 파일은 아래 위치에 보관된다.

이클립스워크스페이스\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\SpringMVC_FildUpload\pds\saveFile




--------------------------------------------------------------------









'자료' 카테고리의 다른 글

JSTL LENGTH 적용  (0) 2013.01.17
SpringMVC_Annotation_iBatis 연동 환경 설정(JAR 파일 포함)  (0) 2012.12.28
Spring, iBatis  (0) 2012.12.28
Struts2 , iBatis 기본 환경설정  (0) 2012.12.28
상담게시판 완료 소스  (0) 2012.12.18

WRITTEN BY
빨강꼬마

,