my code stock.com

Envío y recepción de archivo desde applet

Snippet options

Download: Download snippet as applet.java.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

public class appletUpload{
    
    public void cerrarDocumento(String ruta, String path) {        
        if(documentoPDF.isOpen()){
            documentoPDF.close();
        }        
        
        // Enviar documento creado a FileSystem.
         try {
        
              File file = new File(ruta+".pdf");
             
              FileInputStream in = new FileInputStream(file);
              byte[] buf=new byte[in.available()];
              int bytesread = 0;
             
             System.out.println(file.getName());
             
              String toservlet = path + "/sisep/upload/uploadFile.do?parametro="+file.getName();
             
              URL servleturl = new URL(toservlet);
              URLConnection servletconnection = servleturl.openConnection();
              servletconnection.setDoInput(true);
              servletconnection.setDoOutput(true);
              servletconnection.setUseCaches(false);
              servletconnection.setDefaultUseCaches(false);
              servletconnection.setRequestProperty("Content-Type","application/octet-stream");
              servletconnection.setRequestProperty("Content-Length",Long.toString(file.length()));
              servletconnection.setRequestProperty("fileName", file.getName());
             
              DataOutputStream out=new DataOutputStream(servletconnection.getOutputStream());
             
              while( (bytesread = in.read( buf )) > -1 )
                  {
                  out.write( buf, 0, bytesread );
              }
             
              out.flush();
              out.close();
              in.close();
             
              DataInputStream inputFromClient = new DataInputStream(servletconnection.getInputStream());
              inputFromClient.close();
              file.delete();
          } catch(Exception e) {
              e.printStackTrace();
          }
        
    }//cerrar documento
    
    
    
    /**
     * Recibe el archivo desde el applet.
     * */
    @RequestMapping
    public String uploadFile(Model model, HttpServletRequest req, 
                             HttpServletResponse res) {
                             
        String fileName = req.getParameter("parametro");
        
        String path = pathFile(fileName, true);
        
        path += fileName;
        
        try {

            File yourFile = new File(path);
            FileOutputStream toFile = new FileOutputStream(yourFile);
            DataInputStream fromClient = 
                new DataInputStream(req.getInputStream());

            byte[] buff = new byte[1024];
            int cnt = 0;
            while ((cnt = fromClient.read(buff)) > -1) {
                toFile.write(buff, 0, cnt);
            }
            toFile.flush();
            toFile.close();
            fromClient.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
    
    
    
    
}

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.