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