이거 맘대로 되는 세상이 아니구만...

[JAVA] cos.jar을 이용한 파일업로드 다운로드 입니다. 본문

java

[JAVA] cos.jar을 이용한 파일업로드 다운로드 입니다.

바이홍 2008. 3. 21. 17:57
반응형

이런한 자료는 많은데 제가 직접 조금 수정하고 만들었다는데 의의를 두고있어여 ^^ㅋ

1. File Upload

MultipartRequest multi = null;
   String savepath = request.getRealPath("/") + "WebContent/StyleUpload";

   int limitSize = 15 * 1024 * 1024;
   
   try {
    multi = new MultipartRequest(request, savepath, limitSize, "euc-kr",

                                              new DefaultFileRenamePolicy());
    task = multi.getParameter("task");
    String styleNo = multi.getParameter("styleNo");
    String contents = multi.getParameter("contents");
    String attachFile = multi.getFilesystemName("attachFile");

   
    if (attachFile == null) {
     attachFile = "";
    } else {
     byte[] byteStr = attachFile.getBytes(); 
     int len = byteStr.length;
     if (len > 50) {
      File f = new File(savepath + "/" + attachFile);
      if (f.isFile()) {
       f.delete();
      }
     }
    }

} catch (IOException ie) {
    log.debug("첨부파일 용량 초과!!! 예외발생!!!");
    throw ie;
   }


2.File DownLoad

if(vo.getString("fileDown").equals("true")) {
          request.setCharacterEncoding("EUC-KR");
       String fileName = new String(request.getParameter("fileUrl").getBytes("ISO-8859-1"), "KSC5601");
       String fileUrl = "WebContent/StyleUpload/" + fileName;
       if(fileUrl == null) return;
       boolean fileexists = true;
       try {
        ServletContext cxt = getServletConfig().getServletContext();
        String file = cxt.getRealPath(fileUrl);
        File fileEx = new File(file);
        if(fileEx.exists()) {
           response.setContentType("application/octet-stream");
           response.setHeader("Content-Disposition","attachment; filename="+ java.net.URLEncoder.encode(fileName,"UTF-8"));
           byte [] buffer = new byte[1024*15];
           ServletOutputStream out = response.getOutputStream();
           BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
           BufferedOutputStream bout = new BufferedOutputStream(out);
           int n = 0;
           while((n=in.read(buffer, 0, 1024*15)) != -1) {
                 bout.write(buffer, 0, n);
           bout.flush();
           }
           bout.close();
           in.close();
           } else {
           fileexists = false;
           }
       } catch(Exception e) {
          e.printStackTrace();
          }
          if(!fileexists) {
           vo.put("message", MessageUtil.getMessage("MSG00009"));
          }

      }


3.File Delete

request.setCharacterEncoding("EUC-KR");
      String fileName = new String(request.getParameter("fileUrl").getBytes("ISO-8859-1"), "KSC5601");
      String fileUrl = "WebContent/StyleUpload/" + fileName;
      if(fileUrl == null) return;
      boolean fileexists = true;
      try
      {
       ServletContext cxt = getServletConfig().getServletContext();
       String file = cxt.getRealPath(fileUrl);
       File fileEx = new File(file);
       if(fileEx.exists())
       {
        fileEx.delete();
       }
      }
      catch(Exception e)
      {
          e.printStackTrace();
       } 
반응형
Comments