0

我正在尝试使用 HttpClient 将文件从我的机器上传到另一台机器

这是我的代码:

package com.mxui;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;

public class SampleFileUpload
{
    private static String executeRequest(HttpRequestBase requestBase)
    {
        String responseString = "";
        InputStream responseStream = null;
        HttpClient client = new DefaultHttpClient();
        try{
        HttpResponse response = client.execute(requestBase);
        if(response != null)
        {
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null)
            {
                responseStream = responseEntity.getContent();
                if (responseStream != null)
                {
                    BufferedReader br = new BufferedReader (new InputStreamReader (responseStream));
                    String responseLine = br.readLine();
                    String tempResponseString = "";
                    while (responseLine != null)
                    {
                        tempResponseString = tempResponseString + responseLine + System.getProperty("line.separator");
                        responseLine = br.readLine();
                    }
                    br.close();
                    if (tempResponseString.length()>0)
                    {
                        responseString = tempResponseString;
                    }
                }
            }
        }
        }catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }catch (ClientProtocolException e)
        {
            e.printStackTrace();
        }catch (IllegalStateException e)
        {
            e.printStackTrace();
        }catch (IOException e)
        {
            e.printStackTrace();
        }finally
        {
            if (responseStream != null)
            {
                try {
                    responseStream.close();
                    } catch (IOException e)
                    {
                        e.printStackTrace();
                    }
            }
        }
        client.getConnectionManager().shutdown();
        return responseString;
    }

    public String executeMultiPartRequest(String urlString, File file, String fileName, String fileDescription)
    {
        HttpPost postRequest = new HttpPost(urlString);
        try{
            MultipartEntity multiPartEntity = new MultipartEntity();
            multiPartEntity.addPart("fileDescription", new StringBody(fileDescription != null ? fileDescription : ""));
            multiPartEntity.addPart("fileName", new StringBody(fileName != null ? fileName : file.getName()));
            //FileBody fileBody = new FileBody(file, "application/octect-stream");
            FileBody fileBody = new FileBody(file, "text/plain");
            multiPartEntity.addPart("attachment", fileBody);
            postRequest.setEntity(multiPartEntity);
            }catch (UnsupportedEncodingException ex)
            {
                ex.printStackTrace();
            } 
            return executeRequest(postRequest);
    }

    public static void main(String args[])
    {
        SampleFileUpload fileUpload = new SampleFileUpload();
        File file = new File ("test.txt");
        String response = fileUpload.executeMultiPartRequest("http://192.168.2.21:8080/home/user/Desktop/uploaded-data", file, file.getName(), "File Upload test Hydrangeas.jpg description");
        System.out.println("Response : "+response);
    }
}

但是它的执行和打印响应是空的,它没有上传也请任何人帮忙,我错过了什么谢谢。

4

2 回答 2

0

您必须在另一台机器上也有相应的系统。接受POST请求并将数据存储在系统上的一种。

Apaches Fileupload将为您提供帮助。

于 2013-09-30T13:11:47.653 回答
0
package com.saba.ShRaamVideo;

import java.io.File;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;

public class JavaToRest2
{
    public static void main(String args[]) throws Exception
    {
        long startTime = System.currentTimeMillis();
        doImport();
        long endTime   = System.currentTimeMillis();
        long totalTime = endTime - startTime;
        System.out.println("totalTime :"+totalTime);
    } 
    public static void doImport(){
        try {

         File file = new File("C:/Users/arsingh/Desktop/ShRaamData/Content/Scom/Surviving_Sepsis_cloud1.zip") ;
         //Upload the file

            executeMultiPartRequest("http://localhost/content/nodejs",
                     file, file.getName(), "File Uploading") ;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void executeMultiPartRequest(String urlString, File file, String fileName, String fileDescription) throws Exception
    {
        HttpClient client = new DefaultHttpClient() ;
        HttpPost postRequest = new HttpPost (urlString) ;
        try
        {
            //Set various attributes
            MultipartEntity multiPartEntity = new MultipartEntity () ;
          //  multiPartEntity.addPart("fileDescription", new StringBody(fileDescription != null ? fileDescription : "")) ;
          //  multiPartEntity.addPart("fileName", new StringBody(fileName != null ? fileName : file.getName())) ;

           // FileBody fileBody = new FileBody(file, "application/octect-stream") ;
            FileBody fileBody = new FileBody(file) ;
            //Prepare payload
            multiPartEntity.addPart("file", fileBody) ;

            //Set to request body
            postRequest.setEntity(multiPartEntity) ;

            //Send request
            HttpResponse response = client.execute(postRequest) ;

            //Verify response if any
            if (response != null)
            {
                System.out.println(response.getStatusLine().getStatusCode());
            }
        }
        catch (Exception ex)
        {
            ex.printStackTrace() ;
        }
    }
}
于 2017-11-28T12:06:40.580 回答