在Java中使用HttpURLConnection上载文件时出错
我正在使用Java HttpURLConnection上传一个文件,其中有两个字段,第一个是键,它是纯文本,第二个是文件加载二进制文件。上传这些数据时,我得到了IOException,响应代码为401。我正在为授权设置正确的密钥,但它不起作用。这是我的密码在Java中使用HttpURLConnection上载文件时出错,java,file-upload,httpurlconnection,multipartform-data,Java,File Upload,Httpurlconnection,Multipartform Data,我正在使用Java HttpURLConnection上传一个文件,其中有两个字段,第一个是键,它是纯文本,第二个是文件加载二进制文件。上传这些数据时,我得到了IOException,响应代码为401。我正在为授权设置正确的密钥,但它不起作用。这是我的密码 public FileUpload save() throws MalformedURLException, IOException{ String responseUrl=""; final String boundar
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,所以我从这个链接获得了帮助
请帮我找出错误
这是完整的错误跟踪
java.io.IOException:服务器返回了URL: 位于sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1627) 位于sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254) 在FileUpload.save(CloudFile.java:115) 在CloudFileTest.saveFiletest(CloudFileTest.java:19) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)中 在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中 位于java.lang.reflect.Method.invoke(Method.java:606) 位于org.junit.runners.model.FrameworkMethod$1.runReflectVeCall(FrameworkMethod.java:47) 位于org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 位于org.junit.runners.model.FrameworkMethod.invokeeexplosive(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)上,请在此处复制您的异常详细信息。java.io.IOException:Server返回的HTTP响应代码:401用于URL:请使用
编辑按钮,将信息添加到您的问题中,而不是添加到注释中。此外,需要的是完整的堆栈跟踪(几行),并尝试在代码中找到它所说的错误行,并用注释为我们标记。api似乎需要某种身份验证,而您没有提供这种验证。这不是一个简单的文件上传问题。更新你的tage。是的,它需要一个字符串类型的键。我在上面的代码中提供密钥。