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();
}
} |
Partager