Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.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
Java 如何使用PUT方法将文件发送到web服务_Java_Json_File Upload_Http Put - Fatal编程技术网

Java 如何使用PUT方法将文件发送到web服务

Java 如何使用PUT方法将文件发送到web服务,java,json,file-upload,http-put,Java,Json,File Upload,Http Put,我正在尝试使用PUT方法上载文件。以前有人建议我使用POST并进行多部分上传,但我的web服务只允许GET、PUT、PATCH、DELETE、HEAD和选项。主要原因是我正在更新包含id、目标、源和文件的JSON数组。现在我只需要更新web服务中的文件部分,即,我必须将文件上传到那里 这是我的密码: URLConnection urlconnection = null; try{ File file = new File("/Users/pathtofile/file.p

我正在尝试使用PUT方法上载文件。以前有人建议我使用POST并进行多部分上传,但我的web服务只允许GET、PUT、PATCH、DELETE、HEAD和选项。主要原因是我正在更新包含id、目标、源和文件的JSON数组。现在我只需要更新web服务中的文件部分,即,我必须将文件上传到那里

这是我的密码:

URLConnection urlconnection = null;
    try{
        File file = new File("/Users/pathtofile/file.pdb");
        URL url = new URL("http://example.website/api/jobs/5/");
        urlconnection = url.openConnection();
        urlconnection.setDoOutput(true);
        urlconnection.setDoInput(true);

        if (urlconnection instanceof HttpURLConnection) {
            try {
                ((HttpURLConnection)urlconnection).setRequestMethod("PUT");
                ((HttpURLConnection)urlconnection).setRequestProperty("Content-Type",
                        "application/json");

                ((HttpURLConnection)urlconnection).connect();
            } catch (ProtocolException e) {
                // TODO Auto-generated catch block
                System.out.println("ProtocolException");
                e.printStackTrace();
            }
        }
        BufferedOutputStream bos = new BufferedOutputStream(urlconnection
                .getOutputStream());
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
                file));
        int i;
        // read byte by byte until end of stream
        while ((i = bis.read()) >= 0) {
            bos.write(i);
        }
        bos.close();
        // Getting error here (Bad request).
        System.out.println(((HttpURLConnection)urlconnection).getResponseMessage());

    } catch(Exception e) {
        System.out.println("Exception");
        e.printStackTrace();
    }
    try {
        InputStream inputStream;
        int responseCode = ((HttpURLConnection)urlconnection).getResponseCode();
        if ((responseCode >= 200) && (responseCode <= 202)) {
            inputStream = ((HttpURLConnection)urlconnection).getInputStream();
            int j;
            while ((j = inputStream.read()) >= 0) {
                System.out.println(j);
            }
        } else {
            System.out.println(responseCode);
            inputStream = ((HttpURLConnection)urlconnection).getErrorStream();
            System.out.println(inputStream);
        }
        ((HttpURLConnection)urlconnection).disconnect();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        System.out.println("IOException");
        e.printStackTrace();
    }
有没有使用PUT方法发送文件的方法?文件将放在这样一个地方:

{"target":["This field is required."],
"project":["This field is required."],
"option":["This field is required."],
"result_file":["No file was submitted."],
"com_status":["This field is required."]}

上面是我在没有给出任何参数的情况下尝试的curlput方法的输出。它还显示参数是JSON格式所必需的

我看不出你的代码中有任何明显的问题,你能用curl测试一下发生了什么吗?试着这样做:curl-v-H“Content-Type:application/json”-X PUT-d@/Users/pathtofile/file.pdb不,没有问题。使用curl时,效果非常好。我只是想知道如何上传文件。web服务需要一个JSON数组。而我只发送一个文件。我应该以JSON格式发送所有参数,包括文件。我不知道如何使用PUT。你是说多部分请求吗?如果是,请检查此PUT不使用多部分请求:/PUT仅仅是一个方法名而已,它可以是“FOO”或“BAR”,它实际上只取决于您的服务器。如果我愿意,我可以用GET方法在服务器上推送一些内容。我看不出你的代码中有任何明显的问题,你能用curl测试一下发生了什么吗?试着这样做:curl-v-H“Content-Type:application/json”-X PUT-d@/Users/pathtofile/file.pdb不,没有问题。使用curl时,效果非常好。我只是想知道如何上传文件。web服务需要一个JSON数组。而我只发送一个文件。我应该以JSON格式发送所有参数,包括文件。我不知道如何使用PUT。你是说多部分请求吗?如果是,请检查此PUT不使用多部分请求:/PUT仅仅是一个方法名而已,它可以是“FOO”或“BAR”,它实际上只取决于您的服务器。如果我愿意,我可以用GET方法在服务器上推送一些内容。
{"target":["This field is required."],
"project":["This field is required."],
"option":["This field is required."],
"result_file":["No file was submitted."],
"com_status":["This field is required."]}