/**
*
* @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上传文件的更多相关文章

  1. Java中使用HttpPost上传文件以及HttpGet进行API请求(包含HttpPost上传文件)

    Java中使用HttpPost上传文件以及HttpGet进行API请求(包含HttpPost上传文件) 一.HttpPost上传文件 public static String getSuffix(fi ...

  2. Java模拟http上传文件请求(HttpURLConnection,HttpClient4.4,RestTemplate)

    先上代码: public void uploadToUrl(String fileId, String fileSetId, String formUrl) throws Throwable { St ...

  3. JAVA中使用FTPClient上传下载

    Java中使用FTPClient上传下载 在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件.下载文件,本文简单介绍如何利用jakarta commons中的FTPClient(在c ...

  4. ApiPost接口调试工具模拟Post上传文件(中文版Postman)

    ApiPost简介: ApiPost是一个支持团队协作,并可直接生成文档的API调试.管理工具.它支持模拟POST.GET.PUT等常见请求,是后台接口开发者或前端.接口测试人员不可多得的工具 . A ...

  5. c# 模拟POST上传文件到服务器

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...

  6. asp.net中FileUpload得到上传文件的完整路径

    asp.net中FileUpload得到上传文件的完整路径 Response.Write("完整路径:" + Server.MapPath(FileUpload1.PostedFi ...

  7. ASP.NET、JAVA跨服务器远程上传文件(图片)的相关解决方案整合

    一.图片提交例: A端--提交图片 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string u ...

  8. Spring中servletFileUpload完成上传文件以及文本的处理

    JSP: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnco ...

  9. java中Commons-fileupload实现上传

    java中Commons-fileupload组件实现上传 在实现功能之前需要导入两个jar文件,分别是 commons-fileupload-1.3.1.jar 和 commons-io.jar 文 ...

随机推荐

  1. IP地址转换、主机大小端、htonl、ntohl实现

    copy   #include <IOSTREAM> //#include <WINSOCK.H> using std; typedef  uint16; unsigned   ...

  2. VB6.0中数组的定义实測

    作者:iamlasong 1.环境 Visual Basic 6.0精简版,由于仅仅做一些小工具,认为这个已经够了.我认为.编程语言.数据库等东西,不一定要用最新的,适合.够用就好,比方Win8都出来 ...

  3. hdu-Coins

    http://acm.hdu.edu.cn/showproblem.php?pid=2844 Problem Description Whuacmers use coins.They have coi ...

  4. 使用Sublime经验分享

    Sublime轻量级.可是绝对是神一样的编辑器. 1.代码清晰美观 2.能够选择文件夹作为文件结构文件夹显示在左側 3.以tab的形式打开多个页面在同一个窗体内 设置方法例如以下: Preferenc ...

  5. powerdesigner里建物理模型图时choose DBMS为空怎么办?

    RT 出现如下对话框,是因为需要“DBMS”的规则文件夹 点击下图文件图标,浏览,找到安装目录里面PowerDesigner 15\Resource Files\DBMS,就可以了. 在此记录一下,希 ...

  6. Apatche httpd + Django + Mysql web server 搭建

    Required: httpd: 2.4.18 django : 1.8.7 mysql: 5.7.10 MySQL-python: 1.2.3 mod_wsgi: 4.4.21 Offical Do ...

  7. 在EXCEL中如何让一列数字变成文本格式?就是想让单元格的左上角变一个绿绿的?

    如何在EXCEL中如何让一列数字变成文本格式?就是想让单元格的左上角变一个绿绿的? 解决方案:将整列单元格格式设为文本,然后,选中该列,数据--分列--完成 详细步骤: (1)选中1行或者1列,再单击 ...

  8. 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 ...

  9. taro 自定义 轮播图组件

    1.代码 components/MySwiper/index.js /** * 轮播图组件 */ import Taro, { Component } from '@tarojs/taro'; imp ...

  10. C++ 代码风格准则:POD

    作者:一根筋的傻瓜链接:https://www.zhihu.com/question/36379130/answer/69853366来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载 ...