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();
        }
    }
时间: 2024-07-28 12:19:50

Java中模拟POST上传文件的相关文章

java中Commons-fileupload实现上传

java中Commons-fileupload组件实现上传 在实现功能之前需要导入两个jar文件,分别是 commons-fileupload-1.3.1.jar 和 commons-io.jar 文件,这里就不提供链接,网上搜索都有可以自行下载. ----------------------------------------------------jsp页面----------------------------------------------------- 第一步:先上传一个简单的js

Spring 中Apache Commons上传文件

在用Apache Commons完成文件上传功能需有的几步: 1.导入两个架包:commons-fileupload.jar和commons-io.jar maven中: 1 <dependency> 2 <groupId>commons-fileupload</groupId> 3 <artifactId>commons-fileupload</artifactId> 4 <version>1.3</version> 5

Linux中ftp不能上传文件/目录的解决办法

在linux中不能上传文件或文件夹最多的问题就是权限问题,但有时也不一定是权限问题了,像我就是空间不够用了,下面我来总结一些ftp不能上传文件/目录的解决办法 在排除用户组和权限等问题后,最可能引起ftp下文件无法上传并经常被忽略的因素就是硬盘空间已满,而导致硬盘空间满的问题多数是日志文件所占用的,例如mysql的日志会在不知不觉中变得十分庞大,直至占满剩余的硬盘空间. 解决办法: 1.定期手动清理或使用脚本清理mysql日志或其他相关日志文件. 2.关闭mysql日志(不推荐) 上面是空间有问

本地虚拟机中匿名ftp上传文件失败的问题

在10.10.50.230中新建了一个匿名的ftp服务器,结果在10.10.50.241中上传文件时提示: local: README.txt remote: /var/ftp/pub/upload 227 Entering Passive Mode (10,10,50,230,117,8). 553 Could not create file.   由于搭建步骤是完全按照标准文档建立的,vsftp.conf的设置应该没有问题,检查:getsebool -a|grep ftp 发现: allow

Java中实现FTP上传下载文件的功能,完整代码

一个JAVA 实现FTP功能的代码,包括了服务器的设置模块,并包括有上传文件至FTP的通用方法.下载文件的通用方法以及删除文件.在ftp服务器上穿件文件夹.检测文件夹是否存在等,里面的有些代码对编写JAVA文件上传或许有参考价值,Java FTP主文件代码: package ftpDemo;         import java.io.DataOutputStream;         import java.io.InputStream;         import java.io.Out

Java中向ftp上传、下载文件

最近的一个项目跟ftp打交道很多,经常需要向ftp上传.下载文件,现在总结一下公共方法. 上传 /** * 文件上传 * * @param ip * host * @param port * 端口 * @param userName * 用户名 * @param passWord * 密码 * @param localpath * 上传文件在本地磁盘路径 * @param fileName * 文件名 * @param remotePath * 文件要上传到ftp服务器的路径 * @throws

通过WebClient模拟post上传文件到服务器

写在前面 最近一直在研究sharepoint的文档库,在上传文件到文档库的过程中,需要模拟post请求,也查找了几种模拟方式,webclient算是比较简单的方式. 一个例子 这里写一个简单接受post请求的aspx页面,代码如下: 1 namespace Wolfy.UploadDemo 2 { 3 public partial class Default : System.Web.UI.Page 4 { 5 protected void Page_Load(object sender, Ev

用iFrame模拟Ajax上传文件

前段时间在解决ajax上传文件时折腾了好一阵.直接用$.post上传文本信息肯定是没有问题的.但是$.post直接上传图片是不可行的. 后来看到网上的一些解决方案,有现成的ajax上传文件的封装的方法也有利用flash的.flash确实是个好方法 但是不是每个人都会flash的而且下载下来现成的方法要做修改也不是件易事,且文件相对较大.最后只好模拟iframe来实现.发现相当的简单.泌阳县马奇建材 html: <iframe name="ajaxUpload" style=&qu

Java 后台模拟POST上传

后台模拟POST上传整合 简介:Java后台模拟post请求,发送参数以及上传文件等方式,适用有些公众API接口的调用. 发送请求 后台 package org.lives.platform.upload.utils; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInpu