단일 파일 업로드 및 다운로드 실현

4809 단어 파일 업로드
/**
     *  
     */
    private File upload;

    private String uploadFileName;

    private String uploadContextType;

 

 

 

/**
     *  
     */

    public String getUploadFileUrl() {
        System.out.println("upload="+upload+">>>>uploadFileName="+uploadFileName);
        String destfile = "";
        try {
            if (upload != null && uploadFileName != null) {
                FileInputStream fis = new FileInputStream(upload);
                 destfile = uploadFilePath + uploadFileName;
                File file = new File(uploadFilePath);
                if(!file.exists()){
                    file.mkdir();
                }
                 FileOutputStream fos = new FileOutputStream(destfile);
                 
               
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = fis.read(buffer)) != -1) {
                    fos.write(buffer, 0, len);
                }
                fos.close();
                fis.close();
            }else{
                return "";
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return uploadFileName;
    }

 
 
 
action 호출:
 
  /**
     *  
     * */
    public String accessoryAdd() {
        String uploadIp = AddrIPUtil.getIpAddr(StrutsUtil.getRequest());
        String uploadDate = HmsUtils.getSysTime();
        String accessoryPath = super.getUploadFileUrl();
        accessory.setAccessoryPath(accessoryPath);
        accessory.setUploadIp(uploadIp);
        accessory.setUploadDate(uploadDate);
        accessory.setIsdelete("N");
        super.getAccessoryDao().addAccessory(accessory);
        return "show_list";
    }

 

 
    jsp :
 
          
 <tr>
               <td width="25%"> :</td>
               <td>
               <input type="file" id="accessoryPath" name="upload" value="" />
               <span id="accessoryPath_err" style="color: red;"></span>
               </td>
             </tr>

 
 
 
/**
     *  

      path: 
     */

 

    public  void downloadFile( String path, HttpServletResponse response) {
        String filePath = uploadFilePath + path;
        String fileName = "";
           try {
           if(filePath.lastIndexOf("/") > 0) {
           fileName = new String(filePath.substring(filePath.lastIndexOf("/")+1, filePath.length()).getBytes("GB2312"), "ISO8859_1");
           }else if(filePath.lastIndexOf("\\") > 0) {
           fileName = new String(filePath.substring(filePath.lastIndexOf("\\")+1, filePath.length()).getBytes("GB2312"), "ISO8859_1");
           }

           }catch(Exception e) {}
           FileInputStream fs = null;
           try {
           fs = new FileInputStream(new File(filePath));
           }catch(FileNotFoundException e) {
           e.printStackTrace();
           return;
           }
           response.reset();
           response.setContentType("APPLICATION/OCTET-STREAM");
           response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
           int b = 0;
           try {
           PrintWriter out = response.getWriter();
           while((b=fs.read())!=-1) {
           out.write(b);
           }
           fs.close();
           out.close();
           }catch(Exception e) {
           e.printStackTrace();
           }
          }

 
 
 
action 호출:
 
public void downloadAcc(){
          String path = StrutsUtil.getParameter("path");
          super.downloadFile(path, StrutsUtil.getResponse());
         
          int id = (Integer.parseInt(StrutsUtil.getParameter("id")));
          super.getAccessoryDao().updateDownloadCount(id);
      }

좋은 웹페이지 즐겨찾기