`
sungang_1120
  • 浏览: 309388 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类

单个文件上传与下载实现

 
阅读更多
/**
     * 文件上传对象
     */
    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);
      }

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics