问题描述
我使用Java程序POST方法将文件从一台服务器上传到另一台服务器。但是我得到了异常。
java.io.IOException:在sun.net写入服务器
时出错。在sun.net.www.protocol.http.HttpURLConnection.writeRequests www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:582)
(HttpURLConnection.java:594)
。在sun.net。 www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1216)
在java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:379)
在com.test.rest.HttpURLConnectionExample.TransferFile( HttpURLConnectionExample.java:107)在com.test.rest.HttpURLConnectionExample.main
(HttpURLConnectionExample.java:44)
我有其他的方法可以和服务器进行身份验证。从下面的代码将被调用。当我得到服务器的响应,我得到以上例外。将文件传输到服务器我已经写下了下面的方法。我的样本代码如下:
公共静态无效TransferFile(){
字符串urlStr =HTTP://192.168 .0.8:8600 / audiofile的路径=622080256分之1/ virtualhaircut.mp3\" ;
文件tempFile =新建文件(/ home / MyPath / Workspace / Sample / virtualhaircut.mp3);
BufferedWriter br = null;
HttpURLConnection conn = null;
网址;
尝试{
url = new URL(urlStr);
AuthenticationUser();
conn =(HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod(POST);
conn.setRequestProperty(Content-Type,new MimetypesFileTypeMap()。getContentType(tempFile.getName()));
} catch(MalformedURLException e1){
System.out.println(Malformed);
e1.printStackTrace();
} catch(ProtocolException e){
System.out.println(Protocol);
e.printStackTrace();
catch(IOException e){
System.out.println(IO);
e.printStackTrace();
}
System.out.println(line 69);
FileInputStream fis;
OutputStream fos;
尝试{
System.out.println(line 75);
System.out.println(line 77);
fis = new FileInputStream(tempFile);
fos = conn.getOutputStream();
byte [] buf = new byte [1024 * 2];
int len = 0;
System.out.println(line 80); ((len = fis.read(buf))> 0){
fos.write(buf,0,len);
System.out.println(line 85);
}
System.out.println(line 87);
buf = null;
fos.flush();
fos.close();
fis.close();
$ b $ catch(FileNotFoundException e){
System.out.println();
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
try {
if(conn.getResponseCode()== 200){
System.out.println(here);
$ b catch(IOException e){
e.printStackTrace();
$ b 解决方案可能是因为接收服务器关闭了连接而出现错误,可能是因为您的文件超出了大小限制。你有小文件测试吗?
I am uploading a file from one server to another server using a Java Program 'POST' method. But I am getting below exception.
java.io.IOException: Error writing to server
at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:582)
at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:594)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1216)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:379)
at com.test.rest.HttpURLConnectionExample.TransferFile(HttpURLConnectionExample.java:107)
at com.test.rest.HttpURLConnectionExample.main(HttpURLConnectionExample.java:44)
I have other method who will authenticate with server. Which will be be called from below code. When I am getting response from server, I am getting above exception. To Transfer a file to server I have written below method. My sample code is below:
public static void TransferFile(){
String urlStr = "http://192.168.0.8:8600/audiofile?path=1/622080256/virtualhaircut.mp3";
File tempFile = new File("/home/MyPath/Workspace/Sample/virtualhaircut.mp3");
BufferedWriter br=null;
HttpURLConnection conn = null;
URL url;
try {
url = new URL(urlStr);
AuthenticationUser();
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", new MimetypesFileTypeMap().getContentType(tempFile.getName()));
} catch (MalformedURLException e1) {
System.out.println("Malformed");
e1.printStackTrace();
} catch (ProtocolException e) {
System.out.println("Protocol");
e.printStackTrace();
} catch (IOException e) {
System.out.println("IO");
e.printStackTrace();
}
System.out.println("line 69");
FileInputStream fis;
OutputStream fos;
try {
System.out.println("line 75");
System.out.println("line 77");
fis = new FileInputStream(tempFile);
fos = conn.getOutputStream();
byte[] buf = new byte[1024 * 2];
int len = 0;
System.out.println("line 80");
while ((len = fis.read(buf)) > 0) {
fos.write(buf, 0, len);
System.out.println("line 85");
}
System.out.println("line 87");
buf = null;
fos.flush();
fos.close();
fis.close();
}catch (FileNotFoundException e) {
System.out.println("");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (conn.getResponseCode() == 200) {
System.out.println("here");
}
} catch (IOException e) {
e.printStackTrace();
}
}
解决方案 It is possible that the error ocurred because the receiving server closed the connection, maybe because your file exceeded the size limit. Have you tested with small files?
这篇关于写入服务器时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!