Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you are overcomplicating it. I JUST wrote something similar that takes any file and transfers it through HTTP to a servlet. Just remember: All data is binary (including a text file)-- period. It's up to the given application to interpret that binary data. For example, notepad.exe simply reads a binary set of data and attempts to format it using ASCII standard. So knowing that, simply read a file using a raw stream, set the mime-type of the connection to application/octet-stream and send it via "PUT" method. The servlet code should be reading the file using the raw InputStream from the request and creating the corresponding file in its space.</p> <p>From the client code: </p> <pre><code>URL url = new URL(urlString); HttpURLConnection cnx = (HttpURLConnection)url.openConnection(); cnx.setRequestMethod("PUT"); cnx.setRequestProperty("Content-Type", "application/octet-stream"); // optional request property cnx.setRequestProperty("filename", filename); cnx.setDoOutput(true); cnx.connect(); BufferedInputStream fileIn = new BufferedInputStream(new FileInputStream(fileName), BUFFER_SIZE); BufferedOutputStream out = new BufferedOutputStream(cnx.getOutputStream(), BUFFER_SIZE); byte[] bytes = new byte[BUFFER_SIZE]; int bytesRead; while((bytesRead = fileIn.read(bytes)) != -1){ out.write(bytes, 0, bytesRead); } </code></pre> <p>That's it. </p> <p>Now the servlet code....</p> <pre><code>public void doPut(HttpServletRequest request, HttpServletResponse response){ String filename = (String)request.getHeader("filename"); StringBuilder fullPath = new StringBuilder(100); ServletContext ctxt = getServletContext(); fullPath.append(ctxt.getRealPath("")) .append("uploads\\").append(filename); File f = new File(fullPath.toString()); f.createNewFile(); InputStream in = request.getInputStream(); BufferedOutputStream fileOut = new BufferedOutputStream(new FileOutputStream(f)); byte[] bytes = new byte[BUFFER_SIZE]; int bytesRead; while((bytesRead = in.read(bytes)) != -1){ fileOut.write(bytes, 0, bytesRead); } fileOut.flush(); fileOut.close(); response.setStatus(HttpServletResponse.SC_CREATED); } </code></pre>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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