在Java

时间:2015-05-04 07:24:11

标签: java file-upload httpurlconnection multipartform-data

我正在使用Java HttpURLConnection上传一个文件,其中有两个字段,第一个是,这是一个纯文本,第二个是 fileToUpload a二进制文件。上传这些数据时,我收到 IOException ,响应代码为 401 。我正在使用正确的密钥进行授权,但它无法正常工作。这是我的代码。

public FileUpload save() throws MalformedURLException, IOException{
     String responseUrl="";
     final String boundary = "========"+System.currentTimeMillis() + "========";
     final String CRLF = "\r\n";
     HttpURLConnection connection = (HttpURLConnection)new URL(serverUrl+"/file/"+appId+"/upload").openConnection();
     connection.setDoOutput(true);
     connection.setDoInput(true);
     connection.setUseCaches(false);
     connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
     connection.setRequestProperty("User-Agent", "CodeJava Agent");
     OutputStream output = connection.getOutputStream();
     PrintWriter writer = new PrintWriter(new OutputStreamWriter(output), true);

     //adding key in header
     writer.append("--" + boundary).append(CRLF);
     writer.append("Content-Disposition: form-data; name=\"key\"").append(CRLF);
     writer.append("Content-Type: text/plain; charset=utf-8").append(CRLF);
     writer.append(CRLF);
     writer.append(key).append(CRLF);
     writer.flush();

     //adding file object in header
     writer.append("--" + boundary).append(CRLF);            
     writer.append("Content-Disposition: form-data; name=\"fileToUpload\"; filename=\"" + fileObject.getName() + "\"").append(CRLF);
     writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(fileObject.getName())).append(CRLF);
     writer.append("Content-Transfer-Encoding: binary").append(CRLF);
     writer.append(CRLF);
     writer.flush();

     FileInputStream inputStream = new FileInputStream(fileObject);
     byte[] buffer = new byte[4096];
     int bytesRead = -1;
     while((bytesRead = inputStream.read(buffer)) != -1){
         output.write(buffer, 0, bytesRead);
     }
     output.flush();
     inputStream.close();
     writer.append(CRLF); // CRLF is important! It indicates end of boundary.
     writer.flush();
     writer.append(CRLF).flush();
     writer.append("--" + boundary + "--").append(CRLF);
     writer.close();
     //end of http header
     //System.out.println();
     try{   
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            String inputLine;
            while ((inputLine = in.readLine()) != null) 
                responseUrl += inputLine;
            in.close();
            this.setUrl(responseUrl);
        }catch (IOException e) {
            this.setUrl(null);
            //System.out.println(e);
        }
     return this;
}

因为我是新的java所以我从这个链接http://www.codejava.net/java-se/networking/upload-files-by-sending-multipart-request-programmatically

获得了帮助

请帮我弄清楚这个错误。

这是完整的错误跟踪

java.io.IOException:服务器返回HTTP响应代码:401为URL:https://api.cloudboost.io/file/commentapp/upload     at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1627)     at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)     在FileUpload.save(CloudFile.java:115)     在CloudFileTest.saveFiletest(CloudFileTest.java:19)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     at java.lang.reflect.Method.invoke(Method.java:606)     在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:47)     在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)     在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)     在org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)     在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)     在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)     在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)     在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:238)     在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:63)     在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)     在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:53)     在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:229)     在org.junit.runners.ParentRunner.run(ParentRunner.java:309)     在org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)     在org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)     在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)     在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)     在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)     在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

0 个答案:

没有答案