Created
May 7, 2014 08:52
-
-
Save sjyun/a4461f3959df82ae713f to your computer and use it in GitHub Desktop.
UploadServlet.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.File; | |
| import java.io.FileNotFoundException; | |
| import java.io.FileOutputStream; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.io.OutputStream; | |
| import java.io.PrintWriter; | |
| import javax.servlet.ServletException; | |
| import javax.servlet.annotation.MultipartConfig; | |
| import javax.servlet.annotation.WebServlet; | |
| import javax.servlet.http.HttpServlet; | |
| import javax.servlet.http.HttpServletRequest; | |
| import javax.servlet.http.HttpServletResponse; | |
| import javax.servlet.http.Part; | |
| @WebServlet( urlPatterns="/upload", name="UploadServlet") | |
| @MultipartConfig( | |
| fileSizeThreshold=1024*1024*2, //2mb | |
| maxFileSize=1024*1024*10, //10mb | |
| maxRequestSize=1024*1024*50, //50mb | |
| location="c:/upload" //파일저장위치 | |
| ) | |
| public class UploadServlet extends HttpServlet { | |
| private static final long serialVersionUID = 1L; | |
| public UploadServlet() { | |
| super(); | |
| } | |
| @Override | |
| protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
| } | |
| protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
| //경로 | |
| final String path = request.getParameter("destination"); | |
| //파일 | |
| final Part filePart = request.getPart("file"); | |
| //파일이름 | |
| final String fileName = getFileName(filePart); | |
| OutputStream out = null; | |
| InputStream filecontent = null; | |
| final PrintWriter writer = response.getWriter(); | |
| try{ | |
| out = new FileOutputStream(new File(path + File.separator + fileName )); | |
| filecontent = filePart.getInputStream(); | |
| int read = 0; | |
| final byte[] bytes = new byte[1024]; | |
| while( (read = filecontent.read(bytes)) != -1){ | |
| out.write(bytes, 0, read); | |
| } | |
| System.out.println("new File" + fileName + path + "에 생성되었습니다.") ; | |
| }catch(FileNotFoundException fne){ | |
| }finally{ | |
| if( out != null ){ | |
| out.close(); | |
| }if(filecontent != null ){ | |
| filecontent.close(); | |
| } | |
| if( writer != null){ | |
| writer.close(); | |
| } | |
| } | |
| } | |
| private String getFileName(final Part part) { | |
| final String partHeader = part.getHeader("content-disposition"); | |
| System.out.println("Part Header = {0}"+ partHeader); | |
| for (String content : part.getHeader("content-disposition").split(";")) { | |
| if (content.trim().startsWith("filename")) { | |
| return content.substring( | |
| content.indexOf('=') + 1).trim().replace("\"", ""); | |
| } | |
| } | |
| return null; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment