Note that there are some explanatory texts on larger screens.

plurals
  1. POSending binary data to a servlet
    text
    copied!<p>I am trying send a file to a servlet.</p> <pre><code>function sendToServlet(){ var file = Components.classes["@mozilla.org/file/local;1"]. createInstance(Components.interfaces.nsILocalFile); file.initWithPath("C:\\Documents and Settings\\me\\Meus documentos\\Downloads\\music.mp3"); var boundary = "--------------" + (new Date).getTime(); var stream = Components.classes["@mozilla.org/network/file-input-stream;1"] .createInstance(Components.interfaces.nsIFileInputStream); stream.init(file, 0x04 | 0x08, 0644, 0x04); // file is an nsIFile instance // Send var req = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"] .createInstance(Components.interfaces.nsIXMLHttpRequest); req.open('POST', 'http://localhost:8080/app/server' , false); var contentType = "multipart/form-data; boundary=" + boundary; req.setRequestHeader("Content-Type", contentType); req.send(stream); } </code></pre> <p>The source of javascript: <a href="https://developer.mozilla.org/En/XMLHttpRequest/Using_XMLHttpRequest#Sending_binary_data" rel="nofollow">https://developer.mozilla.org/En/XMLHttpRequest/Using_XMLHttpRequest#Sending_binary_data</a></p> <p>But does not work.</p> <p>Hi, this the serlevt code used:</p> <pre><code>protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub int size = 1024*20000; long sizeFile = 0; File savedFile = null; boolean isMultipart = ServletFileUpload.isMultipartContent(request); if (!isMultipart) { } else { FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); upload.setFileSizeMax(new Long("-1")); List items = null; try { items = upload.parseRequest(request); } catch (FileUploadException e) { e.printStackTrace(); } Iterator itr = items.iterator(); while (itr.hasNext()) { FileItem item = (FileItem) itr.next(); try { if (item.isFormField()) { ; }else{ String itemName = item.getName(); int sizeName = itemName.length(); int end = itemName.indexOf('\n'); int start = itemName.lastIndexOf('\\'); itemName = itemName.substring(start + 1, sizeName-end-1); savedFile = new File("C:\\Documents and Settings\\eric.silva\\Meus documentos\\"+itemName); item.write(savedFile); } } catch (Exception e) { e.printStackTrace(); } } } }//metodo </code></pre> <p>But when i try to send a file the servlet dont create the file sent. Quando eu tento enviar via javascript a requisição é enviada. Mas o arquivo não é criado no lado do servidor. Acredito que o código apresentado no site da MDN esteja incompleto. </p> <p>When I try to send via javascript the request is sent. But the file is not created on the server side. I believe the code shown on the site of the MDN is incomplete.</p>
 

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