Chinaunix首页 | 论坛 | 博客
  • 博客访问: 350805
  • 博文数量: 88
  • 博客积分: 1673
  • 博客等级: 上尉
  • 技术积分: 934
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-20 13:51
文章分类

全部博文(88)

文章存档

2016年(1)

2015年(4)

2014年(3)

2013年(7)

2012年(11)

2011年(1)

2009年(61)

我的朋友

分类: Java

2014-03-03 11:49:59


点击(此处)折叠或打开

  1. PostMethod filePost = new PostMethod(url);
  2.          filePost.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");
  3.          try
  4.          {
  5.          //通过以下方法可以模拟页面参数提交
  6.          //filePost.setParameter("accountId", "4535435asdf");
  7.          //filePost.setParameter("pass", "1234");URLEncoder.encode(param, "UTF-8")
  8.          Part[] parts1 = {new StringPart("param", URLEncoder.encode(param, "UTF-8")), new FilePart(file.getName(), file)};
  9.          filePost.setRequestEntity(new MultipartRequestEntity(parts1,filePost.getParams()));
  10.          HttpClient client = new HttpClient();
  11.          client.getHttpConnectionManager().getParams().setConnectionTimeout(8000);
  12.          int status = client.executeMethod(filePost);
  13.          if (status == HttpStatus.SC_OK)
  14.          {
  15.          return filePost.getResponseBodyAsString();
  16.          // 上传成功
  17.          }
  18.          else
  19.          {
  20.          return "{\"state\":-1,\"msg\":\"上传失败.\",\"data\":[{}]}";
  21.          // 上传失败
  22.          }
  23.          }
  24.          catch (Exception ex)
  25.          {
  26.              return "{\"state\":-1,\"msg\":\"上传失败.\",\"data\":[{}]}";
  27.          }
  28.          finally
  29.          {
  30.              filePost.releaseConnection();
  31.          }
接收文件及其普通文本

点击(此处)折叠或打开

  1. String charset = "UTF-8";
  2.             DiskFileItemFactory factory = new DiskFileItemFactory();
  3.             // 设置内存缓冲区,超过后写入临时文件
  4.             factory.setSizeThreshold(10240000);
  5.             // 设置临时文件存储位置
  6.             String tmpPath = LinkupManager.paramMap.get("voicePath");
  7.             //获取日期
  8.             String ymd = DateHandler.date2String(new Date(), "yyyyMMdd");
  9.             tmpPath=tmpPath+ymd+File.separator;
  10.             //tmpPath = LinkupManager.rootPath+tmpPath;
  11.             File file = new File(tmpPath);
  12.             if(!file.exists()){
  13.                 file.mkdirs();
  14.             }
  15.             factory.setRepository(file);
  16.             ServletFileUpload upload = new ServletFileUpload(factory);
  17.             // 设置单个文件的最大上传值
  18.             upload.setFileSizeMax(10002400000l);
  19.             // 设置整个request的最大值
  20.             upload.setSizeMax(10002400000l);
  21.             upload.setHeaderEncoding(charset);
  22.             request.setCharacterEncoding(charset);
  23.             res.setCharacterEncoding(charset);
  24.         
  25.                 List<?> items = upload.parseRequest(request);
  26.                 if(items==null && items.size()!=2){
  27.                     obj = new ResultObj(ResultObj.FAIL, "上传文件只能由1个文件跟1条内容Json组成.", null);
  28.                     return obj.toJson();
  29.                 }
  30.                 FileItem item = null;
  31.                 for (int i = 0 ;i < items.size(); i++){
  32.                     item = (FileItem) items.get(i);
  33.                     if(item.getFieldName().equals("param")){
  34.                         param = URLDecoder.decode(item.getString(charset), charset);
  35.                         map.put("param", param);
  36.                         if(param==null || param.equals("")){
  37.                             obj = new ResultObj(ResultObj.FAIL, "Json内容不能为空.", null);
  38.                             return obj.toJson();
  39.                         }
  40.                     }
  41.                     // 保存文件
  42.                     if (!item.isFormField() && item.getName().length() > 0) {
  43.                         String suffixName = item.getName().substring(item.getName().lastIndexOf("."));
  44.                         String newName=createPhotoID()+suffixName.toLowerCase();
  45.                         tpmFilePathName = tmpPath + newName;
  46.                         item.write(new File(tpmFilePathName));
  47.                         File big = new File(tpmFilePathName);
  48.                         String md5=MD5FileUtil.getFileMD5String(big);
  49.                         map.put("filePath", File.separator+ymd+File.separator+newName);
  50.                         map.put("MD5Code", md5);
  51.                     }
  52.             }


阅读(778) | 评论(0) | 转发(0) |
0

上一篇:HttpClient 4 - 文件上传

下一篇:jsonp跨域

给主人留下些什么吧!~~