IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Entrée/Sortie Java Discussion :

[Web] Upload fichier via formulaire HTTP


Sujet :

Entrée/Sortie Java

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    3
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2004
    Messages : 3
    Points : 4
    Points
    4
    Par défaut [Web] Upload fichier via formulaire HTTP
    Bonsoir,

    J' essaie d' uploader du texte sur un serveur web via un formulaire HTTP(méthode POST).
    Je ne trouve pas d' exemple pour ce cas précis.

    J' ai essayé:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
     URL url = new URL("http://www.monsite.net/upload.php");
                HttpURLConnection con = (HttpURLConnection)url.openConnection();
                con.setDoInput(true); 
                con.setDoOutput(true); 
                con.setUseCaches(false); 
                con.setRequestMethod("POST");
                con.setRequestProperty("Content-Type","multipart/form-data"); 
                PrintWriter out = new PrintWriter(con.getOutputStream()); 
                out.print("texte à envoyer"); 
                out.flush(); 
                out.close();
    ce qui ne fonctionne pas bien entendu.
    Auriez vous une méthode plus appropriée?

    Merci

    [ Modéré par Bulbo ]
    Ajout d'un tag dans le titre
    Les Règles du Forum

  2. #2
    Membre actif
    Inscrit en
    Juillet 2003
    Messages
    407
    Détails du profil
    Informations forums :
    Inscription : Juillet 2003
    Messages : 407
    Points : 252
    Points
    252
    Par défaut
    voila un exemple :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
     
    import java.awt.*;
    import java.net.*;
    import java.io.*;
    public class srvHead extends java.applet.Applet implements Runnable{
    	String c = "";
     
     
    	public srvHead(){
    		new Thread(this).start();
    	}
    	public void init(){
    //		new Thread(this).start();
    	}
    	public void run(){
    		try{
    // run this as an application first, when it works than make an applet of it
    			URL u = new URL("http://localhost:101/test");
    			URLConnection c = u.openConnection();
    			// post multipart data
    			c.setDoOutput(true);
    			c.setDoInput(true);
    			c.setUseCaches(false);
    			// set some request headers
    			c.setRequestProperty("Connection", "Keep-Alive");
    // TODO: get codebase of the this (the applet) to use for referer
    			c.setRequestProperty("HTTP_REFERER", "http://applet.getcodebase");
    			c.setRequestProperty("Content-Type", "multipart/form-data; boundary=****4353");
    			DataOutputStream dstream = new DataOutputStream(c.getOutputStream());
    // write content to the server, begin with the tag that says a content element is comming
    			dstream.writeBytes("--****4353\r\n");
    // discribe the content, (in this case it's a file)
    			dstream.writeBytes("Content-Disposition: form-data; name=\"myfile\"; filename=\"C:\\myFile.wav\"\r\nContent-Type: application/octet-stream\r\n\r\n");
    // open a file
    			String title = "Frame Title";
    			Frame frame = new Frame("Select a file");
    			FileDialog fd = new FileDialog(frame);
    			frame.setSize(400, 400);
    			fd.show();
    			System.out.println(fd.getDirectory() + fd.getFile());
    			File f = new File(fd.getDirectory() + fd.getFile());
    			FileInputStream fi = new FileInputStream(f);
    // keep reading 1000 bytes from the file
    			byte[] bt = new byte[1000];
    			int cnt = fi.read(bt);
    			while(cnt==bt.length){
    				dstream.write(bt,0,cnt);
    				cnt = fi.read(bt);
    			}
    // send the last bit to the server
    			dstream.write(bt,0,cnt);
    // now close the file and let the web server know this is the end of this form part
    			dstream.writeBytes("\r\n--****4353\r\n");
    // send a form part named TargetURL with the value: /IntranetContent/TelephoneGuide/Upload/
    			dstream.writeBytes("Content-Disposition: form-data; name=\"TargetURL\"\r\n\r\n");
    			dstream.writeBytes("/IntranetContent/TelephoneGuide/Upload/");
    // let the web server know this is the end of this form part
    			dstream.writeBytes("\r\n--****4353\r\n");
    // send a form part named redirectURL with the value: http://none/none
    			dstream.writeBytes("Content-Disposition: form-data; name=\"redirectURL\"\r\n\r\n");
    			dstream.writeBytes("http://none/none");
    // this is the last information part of the multi part request, close the request
    // close the multipart form request
    			dstream.writeBytes("\r\n--****4353--\r\n\r\n");
    			dstream.flush();
    			dstream.close();
    			fi.close();
    			try{
    				DataInputStream in =
    					new DataInputStream(
    						new BufferedInputStream(c.getInputStream()));
    				String sIn = in.readLine();
    				boolean b = true;
    // TODO: this will loop forever unless you make sure your server page
    // sends a last line like "I am done"
    // than you can do wile(sIn.compareTo("I am done")!=0){
    				while(sIn!=null){
    					if(sIn!=null){
    						System.out.println(sIn);
    					}
    					sIn = in.readLine();
    				}
    			}catch(Exception e){
    				e.printStackTrace();
    			}
    		}catch(Exception e){
    			e.printStackTrace();
    		}
    	}
    	public static void main(String[] args) {
    			new srvHead();
    	}
    }

  3. #3
    Membre à l'essai
    Inscrit en
    Avril 2005
    Messages
    15
    Détails du profil
    Informations forums :
    Inscription : Avril 2005
    Messages : 15
    Points : 18
    Points
    18
    Par défaut
    Je ne sais pas si tu as une servlet ou pas mais si c'est le cas regarde ça :

    http://www.servlets.com/cos/

    Cette API te permettra de faire de l'upload de fichier facilement

    Antoine

Discussions similaires

  1. [PHP 5.0] Comment uploader des fichiers via formulaire en PHP
    Par gringo75011 dans le forum Langage
    Réponses: 3
    Dernier message: 27/01/2014, 08h27
  2. Erreur upload fichier via formulaire
    Par Telecaster dans le forum Langage
    Réponses: 2
    Dernier message: 05/02/2009, 11h30
  3. [Upload] Upload de plusieurs fichiers via formulaire
    Par seb67110 dans le forum Langage
    Réponses: 2
    Dernier message: 02/05/2007, 11h55
  4. [Upload] Problème Upload Fichiers via html
    Par Typhon dans le forum Langage
    Réponses: 3
    Dernier message: 11/02/2007, 11h52
  5. [web] upload fichier perl
    Par lilou0210 dans le forum Web
    Réponses: 3
    Dernier message: 04/11/2004, 08h13

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo