Visualizzazione dei risultati da 1 a 8 su 8
  1. #1
    Utente di HTML.it L'avatar di freetom
    Registrato dal
    Nov 2001
    Messaggi
    3,725

    applet o servlet che riesca a caricare file in automatico...

    Cerco di spiegarmi meglio...

    Mi servirebbe il cod di un applet o di una servlet che premendo un apposito bottone "inizia upload automatico" uploadi in modo automatico sempre lo stesso file immagine.jpg da una mia directory locale su una mia directory remota sul mio spazio online sovrascrivendo quello vecchio.

    Ho cercato e ricercato ma per adesso niente ho trovato

    grazie a tutti/e


  2. #2
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    Ci rivediamo di qua:

    Dal forum della sun
    codice:
    URL azCon = new URL("http://tuosito.com/tuoformupload.php");
    HttpURLConnection yc = (HttpURLConnection)azCon.openConnection();
    yc.setDoOutput(true);
    File fi = new File("C:\\tuofiledauppare.jpg"); 
    DataOutputStream printout;
    printout = new DataOutputStream (yc.getOutputStream ());
    String contentType = new String();
    String input;
    FileReader file = new FileReader(fi);
    BufferedReader inFile = new BufferedReader(file);
    while ((input = inFile.readLine()) != null)
    {
    contentType = contentType + input;
    contentType = contentType + "\r\n";
    }
    printout.writeBytes(contentType);
    
    printout.flush ();
    printout.close ();
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  3. #3
    Utente di HTML.it L'avatar di freetom
    Registrato dal
    Nov 2001
    Messaggi
    3,725

    già



    ma senti...

    URL azCon = new URL("http://tuosito.com/tuoformupload.php");

    fa riferimento alla pagina php tuoformupload.php che è capace con un print $fi di stamparmi a video il file tuofiledauppare.jpg?


    e il codice postato come lo incapsulo in un bottone.. di in una pagina php o html o applicazione exe così da poterlo startare...?


    Grazie ancora


  4. #4
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    una cosa alla volta :P

    Allora quello è solamente il brano di codice per fare l'upload vero e proprio.... manca tutto il contorno, ossia l'applet firmata che andrà a contenere il tutto e il form di upload (che sarà scritto nel tuo linguaggio lato server preferito).

    Ravanando meglio nel forum della SUN ne ho tirato fuori questo:
    codice:
    import java.io.*;
    import java.net.*;
    
    /**
     * <code>MultiPartFormOutputStream</code> is used to write
     * "multipart/form-data" to a <code>java.net.URLConnection</code> for
     * POSTing.  This is primarily for file uploading to HTTP servers.
     *
     * @since  JDK1.3
     */
    public class MultiPartFormOutputStream {
            /**
             * The line end characters.
             */
            private static final String NEWLINE = "\r\n";
    
            /**
             * The boundary prefix.
             */
            private static final String PREFIX = "--";
    
            /**
             * The output stream to write to.
             */
            private DataOutputStream out = null;
    
            /**
             * The multipart boundary string.
             */
            private String boundary = null;
    
            /**
             * Creates a new <code>MultiPartFormOutputStream</code> object using
             * the specified output stream and boundary.  The boundary is required
             * to be created before using this method, as described in the
             * description for the <code>getContentType(String)</code> method.
             * The boundary is only checked for <code>null</code> or empty string,
             * but it is recommended to be at least 6 characters.  (Or use the
             * static createBoundary() method to create one.)
             *
             * @param  os        the output stream
             * @param  boundary  the boundary
             * @see  #createBoundary()
             * @see  #getContentType(String)
             */
            public MultiPartFormOutputStream(OutputStream os, String boundary) {
                    if(os == null) {
                            throw new IllegalArgumentException("Output stream is required.");
                    }
                    if(boundary == null || boundary.length() == 0) {
                            throw new IllegalArgumentException("Boundary stream is required.");
                    }
                    this.out = new DataOutputStream(os);
                    this.boundary = boundary;
            }
    
            /**
             * Writes an boolean field value.
             *
             * @param  name   the field name (required)
             * @param  value  the field value
             * @throws  java.io.IOException  on input/output errors
             */
            public void writeField(String name, boolean value)
                            throws java.io.IOException {
                    writeField(name, new Boolean(value).toString());
            }
    
            /**
             * Writes an double field value.
             *
             * @param  name   the field name (required)
             * @param  value  the field value
             * @throws  java.io.IOException  on input/output errors
             */
            public void writeField(String name, double value)
                            throws java.io.IOException {
                    writeField(name, Double.toString(value));
            }
    
            /**
             * Writes an float field value.
             *
             * @param  name   the field name (required)
             * @param  value  the field value
             * @throws  java.io.IOException  on input/output errors
             */
            public void writeField(String name, float value)
                            throws java.io.IOException {
                    writeField(name, Float.toString(value));
            }
    
            /**
             * Writes an long field value.
             *
             * @param  name   the field name (required)
             * @param  value  the field value
             * @throws  java.io.IOException  on input/output errors
             */
            public void writeField(String name, long value)
                            throws java.io.IOException {
                    writeField(name, Long.toString(value));
            }
    
            /**
             * Writes an int field value.
             *
             * @param  name   the field name (required)
             * @param  value  the field value
             * @throws  java.io.IOException  on input/output errors
             */
            public void writeField(String name, int value)
                            throws java.io.IOException {
                    writeField(name, Integer.toString(value));
            }
    
            /**
             * Writes an short field value.
             *
             * @param  name   the field name (required)
             * @param  value  the field value
             * @throws  java.io.IOException  on input/output errors
             */
            public void writeField(String name, short value)
                            throws java.io.IOException {
                    writeField(name, Short.toString(value));
            }
    
            /**
             * Writes an char field value.
             *
             * @param  name   the field name (required)
             * @param  value  the field value
             * @throws  java.io.IOException  on input/output errors
             */
            public void writeField(String name, char value)
                            throws java.io.IOException {
                    writeField(name, new Character(value).toString());
            }
    
            /**
             * Writes an string field value.  If the value is null, an empty string
             * is sent ("").
             *
             * @param  name   the field name (required)
             * @param  value  the field value
             * @throws  java.io.IOException  on input/output errors
             */
            public void writeField(String name, String value)
                            throws java.io.IOException {
                    if(name == null) {
                            throw new IllegalArgumentException("Name cannot be null or empty.");
                    }
                    if(value == null) {
                            value = "";
                    }
                    /*
                    --boundary\r\n
                    Content-Disposition: form-data; name="<fieldName>"\r\n
                    \r\n
                    <value>\r\n
                    */
                    // write boundary
                    out.writeBytes(PREFIX);
                    out.writeBytes(boundary);
                    out.writeBytes(NEWLINE);
                    // write content header
                    out.writeBytes("Content-Disposition: form-data; name=\"" + name + "\"");
                    out.writeBytes(NEWLINE);
                    out.writeBytes(NEWLINE);
                    // write content
                    out.writeBytes(value);
                    out.writeBytes(NEWLINE);
                    out.flush();
            }
    
            /**
             * Writes a file's contents.  If the file is null, does not exists, or
             * is a directory, a <code>java.lang.IllegalArgumentException</code>
             * will be thrown.
             *
             * @param  name      the field name
             * @param  mimeType  the file content type (optional, recommended)
             * @param  file      the file (the file must exist)
             * @throws  java.io.IOException  on input/output errors
             */
            public void writeFile(String name, String mimeType, File file)
                            throws java.io.IOException {
                    if(file == null) {
                            throw new IllegalArgumentException("File cannot be null.");
                    }
                    if(!file.exists()) {
                            throw new IllegalArgumentException("File does not exist.");
                    }
                    if(file.isDirectory()) {
                            throw new IllegalArgumentException("File cannot be a directory.");
                    }
                    writeFile(name, mimeType, file.getCanonicalPath(), new FileInputStream(file));
            }
    
            /**
             * Writes a input stream's contents.  If the input stream is null, a
             * <code>java.lang.IllegalArgumentException</code> will be thrown.
             *
             * @param  name      the field name
             * @param  mimeType  the file content type (optional, recommended)
             * @param  fileName  the file name (required)
             * @param  is        the input stream
             * @throws  java.io.IOException  on input/output errors
             */
            public void writeFile(String name, String mimeType,
                            String fileName, InputStream is)
                            throws java.io.IOException {
                    if(is == null) {
                            throw new IllegalArgumentException("Input stream cannot be null.");
                    }
                    if(fileName == null || fileName.length() == 0) {
                            throw new IllegalArgumentException("File name cannot be null or empty.");
                    }
                    /*
                    --boundary\r\n
                    Content-Disposition: form-data; name="<fieldName>"; filename="<filename>"\r\n
                    Content-Type: <mime-type>\r\n
                    \r\n
                    <file-data>\r\n
                    */
                    // write boundary
                    out.writeBytes(PREFIX);
                    out.writeBytes(boundary);
                    out.writeBytes(NEWLINE);
                    // write content header
                    out.writeBytes("Content-Disposition: form-data; name=\"" + name + "\"; filename=\"" + fileName + "\"");
                    out.writeBytes(NEWLINE);
                    if(mimeType != null) {
                            out.writeBytes("Content-Type: " + mimeType);
                            out.writeBytes(NEWLINE);
                    }
                    out.writeBytes(NEWLINE);
                    // write content
                    byte[] data = new byte[1024];
                    int r = 0;
                    while((r = is.read(data, 0, data.length)) != -1) {
                            out.write(data, 0, r);
                    }
                    // close input stream, but ignore any possible exception for it
                    try {
                            is.close();
                    } catch(Exception e) {}
                    out.writeBytes(NEWLINE);
                    out.flush();
            }
    
            /**
             * Writes the given bytes.  The bytes are assumed to be the contents
             * of a file, and will be sent as such.  If the data is null, a
             * <code>java.lang.IllegalArgumentException</code> will be thrown.
             *
             * @param  name      the field name
             * @param  mimeType  the file content type (optional, recommended)
             * @param  fileName  the file name (required)
             * @param  data      the file data
             * @throws  java.io.IOException  on input/output errors
             */
            public void writeFile(String name, String mimeType,
                            String fileName, byte[] data)
                            throws java.io.IOException {
                    if(data == null) {
                            throw new IllegalArgumentException("Data cannot be null.");
                    }
                    if(fileName == null || fileName.length() == 0) {
                            throw new IllegalArgumentException("File name cannot be null or empty.");
                    }
                    /*
                    --boundary\r\n
                    Content-Disposition: form-data; name="<fieldName>"; filename="<filename>"\r\n
                    Content-Type: <mime-type>\r\n
                    \r\n
                    <file-data>\r\n
                    */
                    // write boundary
                    out.writeBytes(PREFIX);
                    out.writeBytes(boundary);
                    out.writeBytes(NEWLINE);
                    // write content header
                    out.writeBytes("Content-Disposition: form-data; name=\"" + name +
                            "\"; filename=\"" + fileName + "\"");
                    out.writeBytes(NEWLINE);
                    if(mimeType != null) {
                            out.writeBytes("Content-Type: " + mimeType);
                            out.writeBytes(NEWLINE);
                    }
                    out.writeBytes(NEWLINE);
                    // write content
                    out.write(data, 0, data.length);
                    out.writeBytes(NEWLINE);
                    out.flush();
            }
    
            /**
             * Flushes the stream.  Actually, this method does nothing, as the only
             * write methods are highly specialized and automatically flush.
             *
             * @throws  java.io.IOException  on input/output errors
             */
            public void flush() throws java.io.IOException {
                    // out.flush();
            }
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  5. #5
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    codice:
    /**
             * Closes the stream.  
    
             * 
    
             * NOTE: This method MUST be called to finalize the
             * multipart stream.
             *
             * @throws  java.io.IOException  on input/output errors
             */
            public void close() throws java.io.IOException {
                    // write final boundary
                    out.writeBytes(PREFIX);
                    out.writeBytes(boundary);
                    out.writeBytes(PREFIX);
                    out.writeBytes(NEWLINE);
                    out.flush();
                    out.close();
            }
    
            /**
             * Gets the multipart boundary string being used by this stream.
             *
             * @return  the boundary
             */
            public String getBoundary() {
                    return this.boundary;
            }
    
            /**
             * Creates a new <code>java.net.URLConnection</code> object from the
             * specified <code>java.net.URL</code>.  This is a convenience method
             * which will set the <code>doInput</code>, <code>doOutput</code>,
             * <code>useCaches</code> and <code>defaultUseCaches</code> fields to
             * the appropriate settings in the correct order.
             *
             * @return  a <code>java.net.URLConnection</code> object for the URL
             * @throws  java.io.IOException  on input/output errors
             */
            public static URLConnection createConnection(URL url)
                            throws java.io.IOException {
                    URLConnection urlConn = url.openConnection();
                    if(urlConn instanceof HttpURLConnection) {
                            HttpURLConnection httpConn = (HttpURLConnection)urlConn;
                            httpConn.setRequestMethod("POST");
                    }
                    urlConn.setDoInput(true);
                    urlConn.setDoOutput(true);
                    urlConn.setUseCaches(false);
                    urlConn.setDefaultUseCaches(false);
                    return urlConn;
            }
    
            /**
             * Creates a multipart boundary string by concatenating 20 hyphens (-)
             * and the hexadecimal (base-16) representation of the current time in
             * milliseconds.
             *
             * @return  a multipart boundary string
             * @see  #getContentType(String)
             */
            public static String createBoundary() {
                    return "--------------------" +
                            Long.toString(System.currentTimeMillis(), 16);
            }
    
            /**
             * Gets the content type string suitable for the
             * <code>java.net.URLConnection</code> which includes the multipart
             * boundary string.  
    
             * 
    
             * This method is static because, due to the nature of the
             * <code>java.net.URLConnection</code> class, once the output stream
             * for the connection is acquired, it's too late to set the content
             * type (or any other request parameter).  So one has to create a
             * multipart boundary string first before using this class, such as
             * with the <code>createBoundary()</code> method.
             *
             * @param  boundary  the boundary string
             * @return  the content type string
             * @see  #createBoundary()
             */
            public static String getContentType(String boundary) {
                    return "multipart/form-data; boundary=" + boundary;
            }
    public static void main (String[] args) {
              try {
                URL url = new URL("http://miosito.com/doupload.php");
                // create a boundary string
                String boundary = MultiPartFormOutputStream.createBoundary();
                URLConnection urlConn = MultiPartFormOutputStream.createConnection(url);
                urlConn.setRequestProperty("Accept", "*/*");
                urlConn.setRequestProperty("Content-Type",
                                           MultiPartFormOutputStream.getContentType(boundary));
                // set some other request headers...
                urlConn.setRequestProperty("Connection", "Keep-Alive");
                urlConn.setRequestProperty("Cache-Control", "no-cache");
                // no need to connect cuz getOutputStream() does it
                MultiPartFormOutputStream out =  new MultiPartFormOutputStream(urlConn.getOutputStream(), boundary);
                // write a text field element
                //out.writeField("myText", "text field text");
                // upload a file
                out.writeFile("file", "text/plain", new File("C:\\Documents and Settings\\Andrea\\Desktop\\codicefiscale.html"));
                // can also write bytes directly
                //out.writeFile("myFile", "text/plain", "C:\\test.txt",
                //	"This is some file text.".getBytes("ASCII"));
                //out.flush();
                out.close();
                // read response from server
                BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
                String line = "";
                while((line = in.readLine()) != null) {
                  System.out.println(line);
                }
                in.close();
              }
              catch (Exception e) {
                e.printStackTrace();
              }
            }
    }
    Il main l'ho aggiunto io per fare un po' di test, così come suggerito nel post originale stesso.

    La pagina doupload.php è una banalissima cosa del genere:
    codice:
    <?php
      if(isset($_FILES)) {
        $public_folder = "/web/htdocs/www.miosito.com/home/javaupload";
        $docs = $_FILES['file']['name'];
        $tempname = $_FILES['file']['tmp_name'];
        $log ="";    
        $ext = strrchr($docs, ".");
        $newnamedocs = preg_replace("/(\W)+/","_",substr($docs,0,strrpos($docs,"."))).$ext;
        @move_uploaded_file($tempname, $public_folder."/".$newnamedocs);
      }
    ?>
    <html>
    <body>
    <form enctype="multipart/form-data" action="upload.php" method="POST">
        <input name="file" type="file" />
        <input type="submit" value="Send File" />
    </form>
    </body>
    </html>
    In cui faccio un po' di pulizia di caratteri potenzialmente nocivi dal nome del file di destinazione. Funziona... ma ripeto, adesso devi infilarlo dentro un applet firmata e l'applet dovrai andarla a mettere nel tuo sito.
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  6. #6
    Utente di HTML.it L'avatar di freetom
    Registrato dal
    Nov 2001
    Messaggi
    3,725

    Dunque andando giustamente passo passo.. :-)

    Prendendo il codice sopra:

    quello completo di :

    import java.io.*;
    import java.net.*;

    per intenderci...

    e quello (sempre java nel messaggio subito sottostante) a che serve?


    come lo schiaffo dentro un applet gestibile da pagina html o php?

    E un'altra cosina se puoi.. poi... come la firmo sta applet e che significa firmare un'applet?
    :-)

    Grazie per la tua disponibilità e pazienza


  7. #7
    Andrea1979 potresti postare un esempio di applet? Grazie.

  8. #8
    Scusatemi non mi ero accorto che il post era vecchissimo!!!

    ma se mi volete rispondere va bene uguale!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.