Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
&引用;旋度-F“;Java等价物_Java_File_Curl - Fatal编程技术网

&引用;旋度-F“;Java等价物

&引用;旋度-F“;Java等价物,java,file,curl,Java,File,Curl,以下curl命令在java中的等效值是什么: curl -X POST -F "file=@$File_PATH" 我希望使用Java执行的请求是: curl -X POST -F 'file=@file_path' http://localhost/files/ 我试着: HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(_U

以下curl命令在java中的等效值是什么:

curl -X POST -F "file=@$File_PATH"
我希望使用Java执行的请求是:

curl -X POST -F 'file=@file_path' http://localhost/files/ 
我试着:

            HttpClient httpClient = new DefaultHttpClient();        

    HttpPost httpPost = new HttpPost(_URL);

    File file = new File(PATH);

            MultipartEntity mpEntity = new MultipartEntity();
        ContentBody cbFile = new FileBody(file, "bin");
        mpEntity.addPart("userfile", cbFile);

        httpPost.setEntity(mpEntity);

    HttpResponse response = httpClient.execute(httpPost);
    InputStream instream = response.getEntity().getContent();

我昨天遇到了这个问题。下面是一个使用ApacheHTTP库的解决方案

package curldashf;

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.fluent.Request;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.util.EntityUtils;

public class CurlDashF
{
    public static void main(String[] args) throws ClientProtocolException, IOException
    {
        String filePath = "file_path";
        String url = "http://localhost/files";
        File file = new File(filePath);
        MultipartEntity entity = new MultipartEntity();
        entity.addPart("file", new FileBody(file));
        HttpResponse returnResponse = Request.Post(url)
            .body(entity)
            .execute().returnResponse();
        System.out.println("Response status: " + returnResponse.getStatusLine().getStatusCode());
        System.out.println(EntityUtils.toString(returnResponse.getEntity()));
    }
}

根据需要设置文件路径和url。如果使用的不是文件,则可以用ByteArrayBody、InputStreamBody或StringBody替换FileBody。我的特殊情况需要ByteArrayBody,但上面的代码适用于文件。

您到底有什么问题?一点mroe代码会很有帮助,什么是
httpPost
,例如?我正在尝试使用java程序发送curl命令(已经是Linux终端命令)。我已经尝试了multipart,但我不需要上传或下载文件,而是在远程存储库之间进行传输。我们不知道为什么它不起作用。因此,请发布更多的代码(是的,我们都知道什么是
curl
。。)。例如,您不调用任何post方法,因此上面的片段显然无法工作。您至少需要一个HttpURLConnection…我正在使用org.apache.http.client与REST服务器通信。我已经使用cURL提出了请求。我现在要做的是从java程序执行这些请求。问题是,在一个需要进行文件传输的请求中,curl使用-F选项进行传输,问题是如何在java中进行传输?这应该可以: