您的位置:首页 > 编程语言 > Java开发

java上传文件,下载文件

2017-06-29 16:32 183 查看
1。上传文件
1 protected int doTask(BaseForm form) throws AppException, FatalException, NoExistsException, GreaterMaxException {
2         if(log.isDebugEnabled()) {
3             log.debug("entering doTask ...");
4         }
5         // UploadFormの取得
6         UploadForm uploadForm = (UploadForm)form;
7         FormFile uploadFile = uploadForm.getUploadFile();
8         //アップロードファイルのデータが0件の場合
9         if(uploadFile.getFileSize()==0){
10             return CommonConst.RES_OK;
11         }
12         InputStreamReader isr;
13         T101UpFileHis upFileHis = new T101UpFileHis();
14         try {
15             isr = new InputStreamReader(uploadFile.getInputStream());
16             BufferedReader br = new BufferedReader(isr);
17             String str = null;
18             String[] contextArray;
19             Set<String> set = new HashSet<String>();
20             //総件数
21             long allcount = 0;
22             StringBuffer errStr = new StringBuffer("");
23             while((str = br.readLine())!= null){
24                 allcount++;
25                 contextArray = str.split(",");
26                 //データの項目数の判断
27                 if(contextArray.length == 9 ){
28                     //telNo
29                     if(contextArray[0] != null && !"".equals(contextArray[0])){
30                         set.add(contextArray[0]);
31                     } else {
32                         errStr.append("第"+allcount+"行目データは無効です。"+"\r\n");
33                     }
34                 }else{
35                     errStr.append("第"+allcount+"行目データは無効です。"+"\r\n");
36                 }
37             }
38             isr.close();
39             br.close();
40
41             ByteArrayInputStream errInputStream = null;
42             if(!"".equals(errStr.toString())){
43                 errInputStream = new ByteArrayInputStream(errStr.toString().getBytes());
44             }
45             upFileHis = setT101FromUpload(uploadForm, uploadFile, set, allcount,errInputStream);
46             upFileHisDao.save(upFileHis);
47         } catch (Exception e) {
48             e.printStackTrace();
49         }
50
51         if(log.isDebugEnabled()) {
52             log.debug("leaving doTask ...");
53         }
54         return CommonConst.RES_OK;
55     }


2。下载文件

1 if(funcID.equals(LineConst.FUNC_DOWNLOAD)){
2                     this.csvTplDownloadSrv.doService(baseForm);
3                     CsvFileRegForm csvFileRegForm = (CsvFileRegForm)baseForm;
4                     List<M302PrcTpl> prcTplList=csvFileRegForm.getM302PrcTplList();
5                     String tplStr="";
6                     for (int i = 0; i < prcTplList.size(); i++) {
7                         M302PrcTpl m302PrcTpl=prcTplList.get(i);
8                         tplStr+=m302PrcTpl.getItemNM();
9                         if(i<prcTplList.size()-1){
10                             tplStr+=",";
11                         }
12                     }
13
14                     String fileName=System.currentTimeMillis()+"_"+csvFileRegForm.getComProdId()+"_csvTPL.csv";
15                     // エンコードの転換
16                     fileName = URLEncoder.encode(fileName,LineConst.LIST_CREATE_DOWNLOAD_ENCODER);
17                     OutputStream fos = response.getOutputStream();
18                     BufferedOutputStream bos = new BufferedOutputStream(fos);
19                     response.setContentType(CommonConst.CONST_RESPONSE_CONTENTTYPE);
20                     response.setHeader(CommonConst.CONST_REQUEST_CONTENT_DISPOSITION, CommonConst.CONST_RESPONSE_ATTACHEMENT + fileName);
21                     //bos.write(tplStr.getBytes());
22                     bos.write(tplStr.getBytes(Charset.forName(LineConst.LIST_CREATE_DOWNLOAD_ENCODER)));
23
24                     bos.flush();
25                     fos.close();
26                     bos.close();
27                     return null;
28                 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: