文件上传下载(二)

2、后台DS方法

 

//上传DS中的方法

public TmDocAccessory uploadAccessoryPublic(TmDocAccessory tmd,User user,FormFile fileList)
    {
  InputStream isr = null;
  OutputStream os = null;
  //SmbFileOutputStream fo=null;
  DocumentBuilder builder;
  String path = "";
  TmDocAccessory tmda = new TmDocAccessory();
  try {
   DocumentBuilderFactory factory = DocumentBuilderFactory
     .newInstance();
   builder = factory.newDocumentBuilder();
   Document document = builder.parse(new File(this.getClass()
     .getClassLoader().getResource("/LoadFilePath.xml")
     .getPath()));
   Element rootElement = document.getDocumentElement();
   NodeList list = rootElement.getElementsByTagName("Header");
   Element element = (Element) list.item(0);
   path = element.getChildNodes().item(0).getNodeValue();
   //用于上传附件到共享目录
   //SmbFile uploadFile = new SmbFile("smb://administrator:server254639@192.168.0.254/upload_file/");
   //SmbFile uploadFile = new SmbFile(path);
   File uploadFile= new File(path);
   // SmbFile uploadFile = new SmbFile(rmifile); //指定上传文件的位置
   if (!uploadFile.exists() || uploadFile == null) { // 判断指定路径dir是否存在,不存在则创建路径
    uploadFile.mkdirs();
   }

   FormFile tmp = fileList;

   isr = null;
   
   Long randomNum = new Random().nextLong();
   String radomStr = randomNum.toString().substring(2, 5);
   String accessoryAutoName = new Date().getTime() + radomStr;

   byte[] cbuf = new byte[1024];

   // tmp = (FormFile) iter.next();

   String extendedName = this.splitString(tmp.getFileName(), ".");
   String oldName = this.splitStringPath(tmp.getFileName());
   // String oldName = this
   // .splitStringPath(tmp.getName(),File.separator);
   String fileName = accessoryAutoName;
   if (extendedName != null) {
    fileName = accessoryAutoName + "." + extendedName;
   }
   //设置关联id 也就是文档id
   tmda.setTmDocTypeId(tmd.getTmDocTypeId());
   
   tmda.setAccessoryFileName(fileName);
   tmda.setAccessoryOldFileName(oldName);
   int size = tmp.getFileSize();
   tmda.setAccessorySize(new Integer(size));
   tmda.setExtendedName(extendedName);
   tmda.setMemo(tmd.getMemo());
   tmda.setAccessoryTitle(tmd.getAccessoryTitle());
   
   tmda.setCreatedByEmployee(user.getUserId());
   tmda.setCreatedByDept(user.getDeptid());
   Timestamp ts = new Timestamp(System.currentTimeMillis());
   tmda.setCreationDate(ts);
   tmda.setLastUpdatedByEmployee(user.getUserId());
   tmda.setLastUpdatedByDept(user.getDeptid());
   tmda.setLastUpdateDate(ts);

   // 保存数据到附件表
   this.insertTmDocAccessory(tmda);
   isr = tmp.getInputStream();
   System.out.println("path======"+uploadFile.getPath());
   
   os = new FileOutputStream(uploadFile.getPath() + File.separator
     + fileName);
   //fo= new SmbFileOutputStream(uploadFile.getPath() + File.separator+ fileName);
   int i = 0;
   while ((i = isr.read(cbuf, 0, 1024)) != -1) {
    os.write(cbuf, 0, i);
    //fo.write(cbuf, 0, i);
   }
   // }
        } catch (Exception e)
        {
            e.printStackTrace();
        } finally {
            if (isr != null) {
                try {
                    isr.close();
                    isr = null;
                } catch (Exception e) {

                }
            }
            if (os != null) {
                try {
                    os.close();
                    os = null;
                } catch (Exception e) {

                }
            }
           /* if (fo != null) {
                try {
                    fo.close();
                    fo = null;
                } catch (Exception e) {

                }
            }*/
        }
        return tmda;
    }

 

 

//下载方法

 public ActionForward downFile(ActionMapping mapping, ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception
    {
      
     TmFolderForm theForm = (TmFolderForm) form;
        /*String accIdStr=request.getParameter("tmDocAccessoryId");
        Integer accId=0;
       
        if(accIdStr!=null)
        {
            accId=Integer.parseInt(accIdStr.trim().toString());
        }*/
     Integer accId = theForm.getTmDocAccessoryId();
        TmDocAccessory accessoryV =tmDocAccessoryDS.getTmDocAccessory(accId);
        System.out.println("accessoryV======"+accessoryV+"accID"+accId);
        String fullName = accessoryV.getAccessoryOldFileName();
        String fileName=accessoryV.getAccessoryFileName();
        //String filePath=accessoryV.getAccessoryActualPath();
        //SmbFileOutputStream fo=null;
  DocumentBuilder builder;
        String filePath="";
        try {
   DocumentBuilderFactory factory = DocumentBuilderFactory
   .newInstance();
   builder = factory.newDocumentBuilder();
   Document document = builder.parse(new File(this.getClass()
     .getClassLoader().getResource("/LoadFilePath.xml")
     .getPath()));
   Element rootElement = document.getDocumentElement();
   NodeList list = rootElement.getElementsByTagName("Header");
   Element element = (Element) list.item(0);
   filePath = element.getChildNodes().item(0).getNodeValue();
  } catch (RuntimeException e1) {
   e1.printStackTrace();
  }
        File uploadFile = new File("D:/upload_file/"+fileName);   //指定上传文件的位置
        //SmbFile uploadFile=new SmbFile(filePath+fileName); //指定上传文件的位置
        if (!uploadFile.exists() || uploadFile == null)
        {
         //PrintWriter out=response.getWriter();
         //out.print("<script> kk();</script>");
         //out.close();
         return (mapping.findForward("uploadError"));
        }
        else
        {
         FileInputStream fis=null;
          fis = new FileInputStream(filePath + fileName);
         //SmbFileInputStream fis=null;
         //fis=new SmbFileInputStream(filePath+fileName);
          BufferedInputStream bis=new BufferedInputStream(fis);
          response.setContentType("APPLICATION/OCTET-STREAM");
          response.setHeader("Content-Disposition","attachment; filename=/""+new String(fullName.getBytes("gb2312"),"ISO8859-1")+"/"");
          OutputStream os=response.getOutputStream();
          BufferedOutputStream bos=new BufferedOutputStream(os);
          byte[]cbuf=new byte[1024];
  
          while(bis.read(cbuf)!=-1){
              bos.write(cbuf);
          }
          bos.flush();
          try{
              bos.close();
          } catch (IOException e){
              e.printStackTrace();
          }
          try{
              bis.close();
          } catch (IOException e){
              e.printStackTrace();
          }
        }
        return null;
    }

 

3、/LoadFilePath.xml

 

<?xml version="1.0" encoding="GB2312" ?>
<Root>
<Header left="100" Top="30">D:/upload_file/</Header>
<ServiceType>2</ServiceType>
</Root>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值