[Java] probleem met file upload naar PHP script

Pagina: 1
Acties:

  • pjonk
  • Registratie: November 2000
  • Laatst online: 29-12-2025
Ik wil vanuit een Java applicatie een XML document uploaden naar een PHP script.
Echter het probleem is dat PHP de upgeloade file niet ziet.
De data die ik upload is multipart. Een deel bestaat uit een variabele PRINTFORM die ik meestuur het 2e deel bestaat uit het XML bestand.
Ik gebruik de volgende Java code:
code:
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
      // create hardcoded xml.
      StringBuffer xml = new StringBuffer();
      
      xml.append("<?xml version="+'"'+"1.0"+'"'+" encoding="+'"'+"utf-8"+'"'+" ?>");xml.append("\n");
      xml.append("<Certificate1 title="+'"'+"cvo1"+'"'+">");xml.append("\n");
      xml.append("</Certificate1>");xml.append("\n");
      
      try {
        String urlstring = "http://192.168.200.1/script.php"
        URL url = new URL(urlstring);
        
        URLConnection connection = url.openConnection();
        
        connection.setUseCaches(false);
        connection.setDoOutput(true);
        connection.setDoInput(true);
        
        String name="cvo1.xml";

        Multipart mp = new MimeMultipart();
        
        MimeBodyPart bp = new MimeBodyPart();
        bp.setDisposition("form-data; name="+'"'+"PRINTFORM"+'"');
        bp.setText("html");
        
        MimeBodyPart bp1=new MimeBodyPart();
        bp1.setText(xml.toString());
        bp1.setHeader("Content-Type","text/xml");
        bp1.setDisposition("form-data; name"+'"'+"filename"+'"'+"; filename="+'"'+name+'"');
        mp.addBodyPart(bp);
        mp.addBodyPart(bp1);
        
        // print MultiPart om bericht te kunnen zien.
        mp.writeTo(System.out);

        // stuur bericht naar php script
        mp.writeTo(connection.getOutputStream());
        connection.getOutputStream().flush();

        // laat de response zien.
        BufferedReader in = new BufferedReader(
        new InputStreamReader(
        connection.getInputStream()));
        String inputLine;
        StringBuffer result = new StringBuffer();
        
        while ((inputLine = in.readLine()) != null) {
            System.out.println(inputLine);
        }
        
        in.close();
      } catch (Exception e) {
        e.printStackTrace(System.err);
      }

Iemand enig idee wat ik fout doe :?

It’s nice to be important but it’s more important to be nice


Verwijderd

haal die 'mp.writeTo(System.out);' eens weg.
het kan nl. zijn dat die writeTo z'n content achteraf cleared...

heb er verder geen ervaring mee, maar probeer het eens zou ik zeggen...