Note that there are some explanatory texts on larger screens.

plurals
  1. PORead a File, String, Int from servlet send by Java Client
    primarykey
    data
    text
    <p>I am sending a request from java client program to Servlet like below,</p> <pre><code>URL url = new URL("http://localhost:8080/TestWebProject/execute"); HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); httpCon.setDoOutput(true); httpCon.setRequestMethod("POST"); DataOutputStream out = new DataOutputStream(httpCon.getOutputStream()); out.writeUTF("hello"); out.writeUTF("World"); ByteArrayOutputStream bos; File baseFile = new File("C:\\Users\\jp\\Desktop\\mdn.txt"); if (!baseFile.exists()) { System.out.println("File Not Found Correctly"); } FileInputStream fis = new FileInputStream(baseFile); byte[] fileBytes = new byte[1024]; bos = new ByteArrayOutputStream(); while (true) { int ch = fis.read(fileBytes); if (ch != -1) { bos.write(fileBytes, 0, ch); } else { break; } } bos.close(); fis.close(); out.write(bos.toByteArray()); out.writeInt(10); out.close(); </code></pre> <p><br /><br /> <strong><em>*</em>**<em>*</em>**<em>*</em>**<em>*</em></strong><em>SERVLET SIDE</em><strong><em>*</em>**<em>*</em>**<em>*</em>**<em>*</em></strong></p> <pre><code>InputStream is = (InputStream) req.getInputStream(); DataInputStream dis = new DataInputStream(is); System.out.println("NAME US :" + dis.readUTF()); System.out.println("NAME US 1:" + dis.readUTF()); File f = new File("D:\\temp2.txt"); f.createNewFile(); FileOutputStream fos = new FileOutputStream(f); byte[] fileBytes = new byte[1024]; ByteArrayOutputStream bos = new ByteArrayOutputStream(); while (true) { int ch = dis.read(fileBytes); if (ch != -1) { bos.write(fileBytes, 0, ch); } else { break; } } fos.write(bos.toByteArray()); System.out.println(dis.readInt()); </code></pre> <p>I am getting output as Hello World Also File is successfully copied to mentioned location temp2.txt</p> <p>I am getting problem in line System.out.println(dis.readInt()); as EOF reached.</p> <p>Where I am doing mistake, and How to read data from DataInputStream.</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload