upload file from an applet to a servlet
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   dad_dad
Posted On:   Tuesday, December 13, 2011 04:59 AM

i have a java applet that needs to upload file from a client machine to a web server using a servlet but it cannot run i don't konbw what's the problem , i will post the client and server side code for reference: *client code: private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { URL urlservlet; try { urlservlet = new URL("http://localhost:8084/webUp/ser/myServlet"); FileInputStream in = new FileInputStream(jTextField1.getText()); byte[] tab = new byte[2000000]; in.read(tab); in.close(); HttpURLConnection connImage = (HttpURLConnection) urlservlet.openConnection(); connImage.setDoInput(true); connImage.setDoOutput(true); connImage.setUseCaches(false); connImage.setRequestProperty("Content-Type", "application/octet-stream")   More>>

i have a java applet that needs to upload file from a client machine to a web server using a servlet but it cannot run i don't konbw what's the problem , i will post the client and server side code for reference: *client code: private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { URL urlservlet; try { urlservlet = new URL("http://localhost:8084/webUp/ser/myServlet"); FileInputStream in = new FileInputStream(jTextField1.getText()); byte[] tab = new byte[2000000]; in.read(tab); in.close(); HttpURLConnection connImage = (HttpURLConnection) urlservlet.openConnection(); connImage.setDoInput(true); connImage.setDoOutput(true); connImage.setUseCaches(false); connImage.setRequestProperty("Content-Type", "application/octet-stream"); DataOutputStream out = new DataOutputStream(connImage.getOutputStream()); out.write(tab, 0, tab.length); out.flush(); out.close(); } catch (Exception ex) { jTextField2.setText(ex.getMessage().toString()); } } *servlet code: protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { // Récupération du flux entrant java.io.DataInputStream dis = new java.io.DataInputStream(request.getInputStream()); byte[] temp = new byte[2000000]; dis.readFully(temp); dis.close(); // Ecriture du fichier image sur le serveur File cibleServeur = new File("/dalelRepertoire/nomfichier"); FileOutputStream fos = new FileOutputStream(cibleServeur); fos.write(temp); fos.close(); } catch (Exception ex) { out.write(ex.getMessage().toString()); } }

   <<Less
About | Sitemap | Contact