/**
*
* @param url 请求URL
* @param filePath 本地文件地址
* @return
*/
public static String upload(String url,String filePath){
String fdfsPath = "";
try { HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
File file = new File(filePath);
String name = file.getName();
InputStream in = new FileInputStream(file);
MultipartEntity reqEntity = new MultipartEntity();
InputStreamBody inputStreamBody = new InputStreamBody(in,name);
StringBody fileNam = new StringBody(name);
StringBody dateFlag = new StringBody("20160122152301");
StringBody datumType = new StringBody("0");
StringBody uploadWay = new StringBody("0");
StringBody userId = new StringBody("0538");
StringBody tenderId = new StringBody("2315");
StringBody metrialsType = new StringBody("25");
StringBody ip = new StringBody("0.0.0.1");
StringBody driverName = new StringBody("huawei");
StringBody systemVersion = new StringBody("djf");
StringBody position = new StringBody("信息路38", Charset.forName("utf8"));
      //文件流
reqEntity.addPart("datums", inputStreamBody);
reqEntity.addPart("fileName", fileNam);
reqEntity.addPart("dateFlag", dateFlag);
reqEntity.addPart("datumType", datumType);
reqEntity.addPart("uploadWay", uploadWay);
reqEntity.addPart("userId", userId);
reqEntity.addPart("tenderId", tenderId);
reqEntity.addPart("metrialsType", metrialsType);
reqEntity.addPart("ip", ip);
reqEntity.addPart("driverName", driverName);
reqEntity.addPart("systemVersion", systemVersion);
reqEntity.addPart("position", position); httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
int statusCode = response.getStatusLine().getStatusCode(); if(statusCode == HttpStatus.SC_OK){ System.out.println("服务器正常响应....."); HttpEntity resEntity = response.getEntity();
System.out.println(EntityUtils.toString(resEntity));//httpclient自带的工具类读取返回数据 System.out.println(resEntity.getContent()); EntityUtils.consume(resEntity);
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
} /**
* @param args
*/
public static void main(String[] args) {
upload("http://192.168.1.1:8080/xxxImageUpload.action","E:\\weatertest\\002.jpg");
}

图片下载

  

private static void downFile() {
try { String path = "E:\\downurl\\2016022302\\"; File downFileUrl = new File(path);
File[] files = downFileUrl.listFiles();
for (File file:files) {
BufferedReader bfr = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String downParas = null;
while((downParas=bfr.readLine())!=null){
System.out.println("下载参数:"+downParas);
String[] dows = downParas.split("&");
if(dows==null||dows.length<3){
System.out.println("数据不正常downParas:"+downParas);
}else{
String tenderFlod = path+dows[0].trim();
File tender_fold = new File(tenderFlod.trim()); if(!tender_fold.exists()){
System.out.println("创建文件夹:"+tenderFlod.trim());
tender_fold.mkdir();
}
String leiFold = tenderFlod+"\\"+dows[1].trim();
File lei_Fold = new File(leiFold);
if(!lei_Fold.exists()){
System.out.println("创建文件夹:"+leiFold);
lei_Fold.mkdir();
}
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://imagelocal.eloancn.com/xxxdownImg.action");
StringBody fileName = new StringBody(dows[2]);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("imgPath", fileName);//fileName文件名称
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
int statusCode = response.getStatusLine().getStatusCode();
if(statusCode == HttpStatus.SC_OK){
System.out.println("服务器正常响应....."+dows[2].substring(dows[2].lastIndexOf("/")+1)+"下载完成。");
HttpEntity resEntity = response.getEntity();
String savepath = lei_Fold+"//"+dows[2].substring(dows[2].lastIndexOf("/")+1).trim();
FileOutputStream fos = new FileOutputStream(new File(savepath));
resEntity.writeTo(fos);
}
} } } } catch (Exception e) {
e.printStackTrace();
}
}

最新文章

  1. iOS:根据日志去定位网络请求发生的错误是由于服务端造成的,还是客户端造成的?
  2. 转载:[AngularJS系列] 那伤不起的provider们啊~ (Provider, Value, Constant, Service, Factory, Decorator)
  3. iOS高效开发必备的10款Objective-C类库
  4. 捉襟见肘之自定义自拍相机AVFoundation
  5. linux中/etc/fstab文件删除或修改了,导致系统无法启动
  6. 课堂练习:给定一个十进制的正整数,写下从1开始,到N的所有整数,然后数一下其中出现“1”的个数。
  7. JS 之完美运动框架 如何同时改变元素多个属性?
  8. php 日期处理 例子
  9. Ext JS 6学习文档–第1章–ExtJS入门指南
  10. Azure ARM虚拟机部署反恶意软件-安全扩展
  11. ArrayList、HashMap、HashSet源码总结
  12. AndroidEclipse里的视图里想添加SDK Manager但是找不到怎么办?
  13. 使用Configuration Extensions读取配置
  14. Java8 中的 default
  15. JavaScript学习之路-语法
  16. forever start app.js 启动node时,服务访问一次后第二次就不能访问了
  17. Luogu3514 POI2011 Lollipop 递推、构造
  18. HihoCoder - 1483 区间最值
  19. oracle for update和for update nowait的区别 - 转
  20. 基于VMware Workstation搭建开发服务器

热门文章

  1. Vue使用中遇到问题汇总(二)
  2. phantomjs 抓取房产信息
  3. 【Linux】apt-get install 怎么阻止弹出框,使用脚本默认自动安装?
  4. 【Docker】mesos如何修改hostport默认端口范围?
  5. 爪哇国新游记之十三----XML文件读写
  6. 二叉树(9)----打印二叉树中第K层的第M个节点,非递归算法
  7. 如何使用angularjs实现按钮事件
  8. vue 不能检测数组长度 值变化原因解析
  9. OpenERP|ODOO高德地图应用
  10. android与服务端通讯时使用到的GZIP压缩及解压