13

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();
        }


    }
Vishwajit R. Shinde
  • 465
  • 2
  • 5
  • 18
  • In my case, after I have set `connectionTimeout="90000" maxSwallowSize="-1" maxPostSize="-1"` and released many times, one of them will succeed. I don't know why. – pengchy Apr 18 '18 at 03:50

1 Answers1

20

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?

rlinden
  • 2,053
  • 1
  • 12
  • 13
  • Thanks for reply!!! Yes!!! It is uploading small sized files. But for larger file size it is giving above exception. And one important thing is, it is working with curl command on my linux terminal... I am not getting how to fix this issue using my java program – Vishwajit R. Shinde Aug 10 '15 at 14:03
  • 2
    It is not an issue of the java program. It is a configuration issue on the server side. BTW, if it is really that, would you mind accepting the answer? – rlinden Aug 10 '15 at 14:06
  • So, for this, what I have to do? Is there any property in configuration file of the server? which I have to set on a server – Vishwajit R. Shinde Aug 10 '15 at 14:08
  • And, if it is having configuration problem, then why its working with curl command? – Vishwajit R. Shinde Aug 10 '15 at 14:11
  • 1
    I am not an expert in Http connections, but you have to see if curl is sending by the same POST method and getting received the same way. I found this online for you: http://stackoverflow.com/questions/2947683/httprequest-maximum-allowable-size-in-tomcat – rlinden Aug 10 '15 at 14:14
  • 1
    Hope it helps. Would you mind upvoting the answer? Thanks – rlinden Aug 10 '15 at 14:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/86622/discussion-between-vishwajit-r-shinde-and-rlinden). – Vishwajit R. Shinde Aug 10 '15 at 14:30