2014-03-12 3 views
0

업로드 된 이미지의 크기를 조정하고 업로드 폴더의 서버에 저장할 수있는 방법을 알아야합니다.업로드 후 크기 조정 JSP

사람들이 파일을 업로드 할 수있는 양식을 처리해야합니다.

public void init() { 
    fileSavePath = getServletContext().getRealPath("/") + File.separator + UPLOAD_DIRECTORY;/*save uploaded files to a 'Upload' directory in the web app*/ 
    if (!(new File(fileSavePath)).exists()) { 
     (new File(fileSavePath)).mkdir(); // creates the directory if it does not exist   
    } 
} 


public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { 


     String resp = ""; 
     int i = 1; 
     resp += "<br>Here is information about uploaded files.<br>"; 
     try { 
      MultipartParser parser = new MultipartParser(request, 1024 * 1024 * 1024); /* file limit size of 1GB*/ 
      Part _part; 
      while ((_part = parser.readNextPart()) != null) { 
       if (_part.isFile()) { 
        FilePart fPart = (FilePart) _part; // get some info about the file 
        String name = fPart.getFileName(); 
        if (name != null) { 
         long fileSize = fPart.writeTo(new File(fileSavePath)); 
         resp += i++ + ". " + fPart.getFilePath() + "[" + fileSize/1024 + " KB]<br>"; 
        } else { 
         resp = "<br>The user did not upload a file for this part."; 
        } 
       } 
      }// end while 
     } catch (java.io.IOException ioe) { 
      resp = ioe.getMessage(); 
     } 
     request.setAttribute("message", resp); 
     getServletContext().getRequestDispatcher("/index.jsp").forward(request, response); 
    } 

예를 들어 내가 먼저 한 다음 다시 저장 한 후, 다음 규모를로드, 저장 100 × 100

답변

0

모든 이미지를 rezise해야합니다.

try 
{ 
    BufferedImage img = ImageIO.read(new File(in_path)); 
    Image scaled = img.getScaledInstance(100, 100, Image.SCALE_FAST); 
    ImageIO.write(scaled, "png", out_path); 
} 
catch (Exception ex) 
{ 
    System.out.println(ex.getMessage(); 
} 

의 getScaledInstance는

의 세번째 파라미터에 사용될 수있는 스케일링 방법의 목록을 참조 http://docs.oracle.com/javase/7/docs/api/java/awt/Image.html
관련 문제