File Upload / Download - PowerPoint PPT Presentation

slide1 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
File Upload / Download PowerPoint Presentation
Download Presentation
File Upload / Download

play fullscreen
1 / 23
File Upload / Download
202 Views
Download Presentation
anjelita-ortiz
Download Presentation

File Upload / Download

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. File Upload / Download FORM SUBMIT A Controller Service Dao AJAX B Controller NAS DB

  2. File Upload / Download ㅇ 필요 라이브러리 - commons-fileupload.jar - commons-io.jar - jquery.form.js ㅇDB Table

  3. File Upload / Download ㅇ프로젝트 전체 구조 ㅇPackage 구조

  4. dispatcher-servlet.xml (1 / 3) <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <context:component-scan base-package="mvc.spring" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean>

  5. dispatcher-servlet.xml (2 / 3) <!-- MessageConverter --> <bean class="org.springframework.web.servlet.mvc.annotation. AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter. StringHttpMessageConverter" /> <bean class="org.springframework.http.converter.json. MappingJacksonHttpMessageConverter" /> </list> </property> </bean> <!-- MessageConverter --> <!-- Multipart Resolver --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons. CommonsMultipartResolver"> <!-- maximum file size in bytes 50MB --> <property name="maxUploadSize" value="52428800" /> </bean> <!-- Multipart Resolver -->

  6. dispatcher-servlet.xml (3 / 3) <!-- Properties --> <util:properties id="config" location="/WEB-INF/config.properties" /> <!-- Properties --> <!-- Data Source --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="#{config['db.driver']}" /> <property name="url" value="#{config['db.url']}" /> <property name="username" value="#{config['db.id']}" /> <property name="password" value="#{config['db.pw']}" /> </bean> <!-- Data Source --> <!-- Jdbc Template --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <constructor-arg ref="dataSource" /> </bean> <!-- Jdbc Template --> </beans>

  7. config.properties /WEB-INF db.driver=oracle.jdbc.driver.OracleDriver db.url=jdbc:oracle:thin:@localhost:1521:xe db.id=scott db.pw=tiger

  8. FileController(1 / 3) Package : mvc/spring/controller @Controller public class FileController { @Autowired FileServicefileService; /* submit file upload form */ @RequestMapping(value="fileUpload.do", method=RequestMethod.GET) public ModelAndViewfileUploadForm() { ModelAndViewmav = new ModelAndView(); mav.setViewName("file/fileUpload_submit"); return mav; } /* submit file upload */ @RequestMapping(value="fileUpload.do", method=RequestMethod.POST) public ModelAndViewfileUpload(MultipartRequestmRequest) { ModelAndViewmav = new ModelAndView(); boolean result = fileService.uploadFile(mRequest); mav.addObject("result", result); mav.setViewName("file/fileUpload_result"); return mav; }

  9. FileController(2 / 3) Package : mvc/spring/controller /* ajax file upload form */ @RequestMapping(value="fileUploadAjax.do", method=RequestMethod.GET) public ModelAndViewfileUploadAjaxForm() { ModelAndViewmav = new ModelAndView(); View view = new InternalResourceView( “/WEB-INF/views/file/fileUpload_ajax.jsp"); mav.setView(view); return mav; } /* ajax file upload */ @RequestMapping(value="fileUploadAjax.do", method=RequestMethod.POST) @ResponseBody public Map<String, Object> fileUploadAjax(MultipartRequestmRequest) { boolean result = fileService.uploadFile(mRequest); Map<String, Object> map = new HashMap<String, Object>(); map.put("result", result); return map; }

  10. FileController(3 / 3) Package : mvc/spring/controller /* file download */ @RequestMapping("fileDownload.do") public ModelAndViewfileDownload(@RequestParam("seqNo") String seqNo, @RequestParam("num") String num) { ModelAndViewmav = new ModelAndView(); Map<String, Object> map = new HashMap<String, Object>(); map.put("seqNo", seqNo); map.put("num", num); List<Map<String, Object>> list = fileService.getFileList(map); Map<String, Object> data = new HashMap<String, Object>(); String fileName = (String) list.get(0).get("DSP_FILE_NAME"); String filePath = list.get(0).get("FILE_PATH") + File.separator+ list.get(0).get("FILE_NAME"); File file = new File(filePath); data.put("file", file); data.put("fileName", fileName); mav.addObject("data", data); mav.setView(new FileDownloadView()); return mav; } }

  11. FileService Package : mvc/spring/service public interface FileService { public booleanuploadFile(MultipartRequestmRequest); public List<Map<String, Object>> getFileList(Map<String, Object> map); }

  12. FileServiceImpl (1 / 4) Package : mvc/spring/service @Service public class FileServiceImpl implements FileService { @Autowired FileAtchDaofileAtchDao; @Override public booleanuploadFile(MultipartRequestmRequest) { booleanisSuccess = false; String uploadPath = "/spring/upload"; File dir = new File(uploadPath); if(!dir.isDirectory()) { dir.mkdirs(); }

  13. FileServiceImpl (2 / 4) Package : mvc/spring/service Iterator<String> iter = mRequest.getFileNames(); while(iter.hasNext()) { // HTML input tag name String htmlName = iter.next(); // Multipart File MultipartFilemFile = mRequest.getFile(htmlName); // Original File name String oFileName = mFile.getOriginalFilename(); // Save File name String saveFileName = oFileName; // Save File size long fileSize = mFile.getSize(); String realFilePath = uploadPath + File.separator + saveFileName; if(!saveFileName.equals("") && new File(realFilePath).exists()) { saveFileName = saveFileName + "_" + System.currentTimeMillis(); realFilePath = uploadPath + File.separator + saveFileName; }

  14. FileServiceImpl (3 / 4) Package : mvc/spring/service try { Map<String, Object> map = new HashMap<String, Object>(); map.put("seqNo", 1); map.put("filePath", uploadPath); map.put("fileName", saveFileName); map.put("dspFileName", oFileName); map.put("fileSize", fileSize); fileAtchDao.insertFileAtch(map); mFile.transferTo(new File(realFilePath)); isSuccess = true; } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return isSuccess; }

  15. FileServiceImpl (4 / 4) Package : mvc/spring/service @Override public List<Map<String, Object>> getFileList(Map<String, Object> map) { List<Map<String, Object>> list = fileAtchDao.selectFileAtch(map); return list; } }

  16. FileAtchDao Package : mvc/spring/dao public interface FileAtchDao { public booleaninsertFileAtch(Map<String, Object> map); public List<Map<String, Object>> selectFileAtch(Map<String, Object> map); }

  17. FileAtchDaoImpl (1 / 2) Package : mvc/spring/dao @Repository public class FileAtchDaoImpl implements FileAtchDao { @Autowired JdbcTemplate template; @Override public booleaninsertFileAtch(Map<String, Object> map) { StringBuffersql = new StringBuffer(); sql.append("INSERT INTO FILE_ATCH"); sql.append(" (NUM, SEQ_NO, FILE_PATH, FILE_NAME, DSP_FILE_NAME, FILE_SIZE)"); sql.append(" VALUES "); sql.append(" ((SELECT NVL(MAX(TO_NUMBER(NUM)), 0) + 1 FROM FILE_ATCH), ?, ?, ?, ?, ?) "); int result = template.update(sql.toString(), map.get("seqNo"), map.get("filePath"), map.get("fileName"), map.get("dspFileName"), map.get("fileSize")); return result > 0 ? true : false; }

  18. FileAtchDaoImpl (2 / 2) Package : mvc/spring/dao @Override public List<Map<String, Object>> selectFileAtch(Map<String, Object> map) { StringBuffersql = new StringBuffer(); sql.append("SELECT NUM, SEQ_NO, FILE_PATH"); sql.append(" , FILE_NAME, DSP_FILE_NAME, FILE_SIZE"); sql.append(" FROM FILE_ATCH"); sql.append(" WHERE NUM = ? AND SEQ_NO = ?"); List<Map<String, Object>> list = template.queryForList(sql.toString(), map.get("num"), map.get("seqNo")); return list; } }

  19. fileUpload_submit.jsp /WEB-INF/views/file <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>파일 업로드 화면</title> </head> <body> <form action="./fileUpload.do" method="post" enctype="multipart/form-data"> 파일첨부 1 : <input type="file" name="file1" /> <br /> 파일첨부 2 : <input type="file" name="file2" /> <br /> 파일첨부 3 : <input type="file" name="file3" /> <br /> <input type="submit" value="upload" /> </form> </body> </html>

  20. fileUpload_ajax.jsp (1 / 2) /WEB-INF/views/file <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>파일 업로드 화면</title> <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> <script src="./js/jquery.form.js"></script> <script> $(document).ready(function() { $("input[type=submit]").bind("click", function() { $("form").ajaxSubmit({ success : function(data) { alert(data.result); }, error : function(error) { alert("요청 처리 중 오류가 발생하였습니다."); } }); return false; }); }); </script> </head>

  21. fileUpload_ajax.jsp (2 / 2) /WEB-INF/views/file <body> <form action="./fileUploadAjax.do" method="post" enctype="multipart/form-data"> 파일첨부 1 : <input type="file" name="file1" /> <br /> 파일첨부 2 : <input type="file" name="file2" /> <br /> 파일첨부 3 : <input type="file" name="file3" /> <br /> <input type="submit" value="upload" /> </form> </body> </html>

  22. fileUpload_result.jsp /WEB-INF/views/file <%@ page language="java" contentType="text/html; charset=UTF-8“ pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>파일 업로드 결과</title> </head> <body> <h1>${result}</h1> </body> </html>

  23. FileDownloadView Package : mvc/spring/view public class FileDownloadView extends AbstractView { @Override protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequestrequest, HttpServletResponse response) throws Exception { Map<String, Object> data = (Map<String, Object>) model.get("data"); File file = (File) data.get("file"); String oFileName = (String) data.get("fileName"); response.setContentType(this.getContentType()); response.setContentLength((int)file.length()); response.setHeader("Content-Disposition", "attachment; filename=\"" + java.net.URLEncoder.encode(oFileName, "UTF-8") + "\";charset=\"UTF-8\""); response.setHeader("Content-Transfer-Encoding", "binary"); response.setHeader("Pragma", "no-cache"); OutputStream out = response.getOutputStream(); FileInputStream in = null; in = new FileInputStream(file); FileCopyUtils.copy(in, out); out.close(); out.flush(); } }