macOsX
macOsX

Reputation: 457

Issue in writing data into a file using Servlet

I'm trying to read an XML file and send to the local server using HttpPost. When reading the data at the server side and writing into a file always last few lines are missing.

Client code :

  HttpClient httpclient = new DefaultHttpClient();
  HttpPost httppost = new HttpPost("http://xxx.xxx.xxx.xxx:yyyy/FirstServlet/HelloWorldServlet");      
  InputStreamEntity reqEntity = new InputStreamEntity(
  new FileInputStream(dataFile), -1);
  reqEntity.setContentType("binary/octet-stream");

  // Send in multiple parts if needed
  reqEntity.setChunked(true);
  httppost.setEntity(reqEntity);
  HttpResponse response = httpclient.execute(httppost);
  int respcode =  response.getStatusLine().getStatusCode();

Server code :

    response.setContentType("binary/octet-stream");
    InputStream is = request.getInputStream();
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("C:\\Files\\copyFile.xml")));
    byte[] buf = new byte[4096];
    for (int nChunk = is.read(buf); nChunk!=-1; nChunk = is.read(buf))
    {
        bos.write(buf, 0, nChunk);
    } 

I tried using BufferedReader as well, but same issue.

    BufferedReader in = new BufferedReader(  
            new InputStreamReader(request.getInputStream()));
    response.setContentType("binary/octet-stream");  
    String line = null;  
         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("C:\\Files\\copyFile.xml")));
         while((line = in.readLine()) != null) {
             line = in.readLine();
             bos.write((line + "\n").getBytes());
    }  

I tried using scanner as well. In this case it's working fine only when I use StringBuilder and passing the value again to the BufferedOutputStream.

    response.setContentType("binary/octet-stream");
    StringBuilder stringBuilder = new StringBuilder(2000);
    Scanner scanner = new Scanner(request.getInputStream());
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("C:\\Files\\copyFile.xml")));
    while (scanner.hasNextLine()) {
        stringBuilder.append(scanner.nextLine() + "\n");
    }
    String tempStr = stringBuilder.toString();
    bos.write(tempStr.getBytes());

I can't use the above logic for processing very large XML's since converting to string value will throw Java heap space error.

Kindly let me know what is the issue with the code?

Thanks in advance!

Upvotes: 0

Views: 555

Answers (1)

radai
radai

Reputation: 24192

flush() and close() your output streams. what happens is that youre not flushing and the last few lines remain in some internal buffer and are not written out.

so in your server code:

response.setContentType("binary/octet-stream");
InputStream is = request.getInputStream();
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("C:\\Files\\copyFile.xml")));
byte[] buf = new byte[4096];
for (int nChunk = is.read(buf); nChunk!=-1; nChunk = is.read(buf)) {
   bos.write(buf, 0, nChunk);
}
bos.flush();
bos.close();

Upvotes: 1

Related Questions