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 文 ...
随机推荐
- request.startAsync()不支持异步操作
Servlet3.0使用异步处理时,后台报错: java.lang.IllegalStateException: A filter or servlet of the current chain do ...
- [Python爬虫] 之十三:Selenium +phantomjs抓取活动树会议活动数据
抓取活动树网站中会议活动数据(http://www.huodongshu.com/html/index.html) 具体的思路是[Python爬虫] 之十一中抓取活动行网站的类似,都是用多线程来抓取, ...
- 【实践】js实现windows系统日历
思路:1.定义好每一个月份的日期天数 2.获取当前的系统日期初始化数据 3.输出日历 2.1.先获取当前月的第一天是星期几(这一点与日历的排版至关重要!) 2.2.获取当前月的天数 ...
- linux 设置 亮度
调整系统运行的配置文件位于 /sys/class/ 文件夹下. 调整亮度在 /sys/class/backlight/XXXX/brightness XXXX 是因为使用的驱动不一样,名称就会不一样, ...
- C#运行原理——我的柔情你永远不懂
记得歌手陈琳曾经在1993年发行了第一张专辑<你的柔情我永远不懂>,创造了150万张的销售纪录,里边的主打歌——我的柔情你永远不懂,多年以后才发现是写给C#运行原理的,因为原理总是伤不起~ ...
- xcode arc 下使用 block警告 Capturing [an object] strongly in this block is likely to lead to a retain cycle” in ARC-enabled code
xcode arc 下使用 block警告 Capturing [an object] strongly in this block is likely to lead to a retain cyc ...
- [springMVC - 1A] - Request processing failed; nested exception is org.apache.ibatis.builder.IncompleteElementException
一月 14, 2016 1:30:07 下午 org.apache.catalina.core.StandardWrapperValve invoke严重: Servlet.service() for ...
- B2C电子商务系统研发——产品媒体常见功能点
产品媒体常见功能点 电商研发系列——产品媒体常见功能点 支持图片.视频和文档等媒体类型 产品图片对清晰度要求比极高,但又不能太大,所以图片一般是jpg格式. 视频一般是flv流媒体格式,如果是嵌入产品 ...
- CodeIgniter 无法上传 CSV 文件
本篇文章由:http://xinpure.com/codeigniter-unable-to-upload-a-csv-file/ 解决 CodeIgniter 中使用 Upload 类无法上传 CS ...
- jpush推送
1.下载这个压缩包,vendo文件夹,复制里面的vendo到该文件夹下. 解压 2.修改AppKey和MasterSecret就ok 了