Java中模拟POST上传文件
/**
*
* @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();
}
}
Java中模拟POST上传文件的更多相关文章
- Java中使用HttpPost上传文件以及HttpGet进行API请求(包含HttpPost上传文件)
Java中使用HttpPost上传文件以及HttpGet进行API请求(包含HttpPost上传文件) 一.HttpPost上传文件 public static String getSuffix(fi ...
- Java模拟http上传文件请求(HttpURLConnection,HttpClient4.4,RestTemplate)
先上代码: public void uploadToUrl(String fileId, String fileSetId, String formUrl) throws Throwable { St ...
- JAVA中使用FTPClient上传下载
Java中使用FTPClient上传下载 在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件.下载文件,本文简单介绍如何利用jakarta commons中的FTPClient(在c ...
- ApiPost接口调试工具模拟Post上传文件(中文版Postman)
ApiPost简介: ApiPost是一个支持团队协作,并可直接生成文档的API调试.管理工具.它支持模拟POST.GET.PUT等常见请求,是后台接口开发者或前端.接口测试人员不可多得的工具 . A ...
- c# 模拟POST上传文件到服务器
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...
- asp.net中FileUpload得到上传文件的完整路径
asp.net中FileUpload得到上传文件的完整路径 Response.Write("完整路径:" + Server.MapPath(FileUpload1.PostedFi ...
- ASP.NET、JAVA跨服务器远程上传文件(图片)的相关解决方案整合
一.图片提交例: A端--提交图片 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string u ...
- Spring中servletFileUpload完成上传文件以及文本的处理
JSP: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnco ...
- java中Commons-fileupload实现上传
java中Commons-fileupload组件实现上传 在实现功能之前需要导入两个jar文件,分别是 commons-fileupload-1.3.1.jar 和 commons-io.jar 文 ...
随机推荐
- Oracle判断两个时间段是否相交
SQL中常常要判断两个时间段是否相交,该如何判断呢?比如两个时间段(S1,E1)和(S2,E2).我最先想到的是下面的方法一.方法一:(S1 BETWEEN S2 AND E2) OR (S2 BET ...
- http://www.cnblogs.com/CBDoctor/p/4459750.html
http://www.cnblogs.com/CBDoctor/p/4459750.html
- VB6.0 中 碰到 提示rs对象关闭,不允许操作, 吃一堑长一智
VB6.0 中 碰到 提示rs对象关闭,不允许操作, 吃一堑长一智 如果碰到 这个错误提示 则请检查 你执行的每一个语句中的数据类型,包括 如果有存储过程的话,还要检查 存储过程中每一个变量和数据表 ...
- (转)nio 字符集
字符集 概述 根据 Sun 的文档,一个 Charset 是“十六位 Unicode 字符序列与字节序列之间的一个命名的映射”.实际上,一个 Charset 允许您以尽可能最具可移植性的方式读写字符序 ...
- [Angular] AfterContentChecked && AfterViewChecked
AfterContentChecked & AfterViewChecked are called after 'OnChanges' lifecycle. And each time 'ng ...
- redis学习笔记——RDB和AOF持久化二
上一篇对RDB的源码分析是比较多的,但是AOF持久化执行进行了一些理论上的分析和概念的说明.本来想自己偷一些懒,将上篇文章中最后所给链接的AOF实现代码随便过一过算了,后来也就是在过的过程中发现自己这 ...
- 云计算之路-试用Azure:上不了高速的跑车,无法跨Cloud Service的DNS服务器
从阿里云的踩坑大师,到Azure的抹黑大师,我们似乎成了云计算负面用户的典型,可是我们还是忍不住想表达自己真实的使用感受.如果有错误的地方,欢迎大家批评! 在Azure上建好虚拟网(Vitual Ne ...
- 腾讯技术分享:Android版手机QQ的缓存监控与优化实践
本文内容整理自公众号腾讯Bugly,感谢原作者的分享. 1.问题背景 对于Android应用来说,内存向来是比较重要的性能指标.内存占用过高,会影响应用的流畅度,甚至引发OOM,非常影响用户体验.因此 ...
- iOS APP 上传
原地址:http://www.cnblogs.com/uvsjoh/archive/2012/11/14/2769739.html 流程:1 开发好要发布的程序 -- 需要在程序中包含符合要求规格的i ...
- cordova百度地图定位Android版插件
本插件利用百度地图提供的定位功能进行Android版手机定位. 为什么没有iOS版? 因为iOS版有官方的定位插件cordova-plugin-geolocation可以使用. 请参照:cordova ...