Hello!
My friend and I are working on a little program. It sends a local file to a
server (php), we use the rfc1867 protocoll for this. When the server gets
the data, it replies with some details about the transaction. Here is an
output-example:
********************************************
E:\Studium\Multi\Projekt>java transfer
size: 225308
HTTP/1.1 200 OK
Date: Wed, 26 Jan 2005 16:32:18 GMT
Server: Apache/1.3.33 (Unix) PHP/5.0.2
X-Powered-By: PHP/5.0.2
Connection: close
Content-Type: text/html
<html>
_FILES:
Array
(
[datafile] => Array
(
[name] => test.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpDjKQ1Y
[error] => 0
[size] => 225109
)
)
</html>
**********************************************
First, you see the size of the file (225308 bytes), the there is the server
response. The big problem is, that the server only received 225109 bytes. We
do not know, why this happens. We hope, that somebody can help us.
Here is the source, of the little program:
*********************************
import java.awt.*;
import java.applet.*;
import java.io.*;
import java.util.*;
import java.net.*;
//import java.exception.*;
public class transfer
{
// Construct data
public static void main (String args[]) {
try{
String hostname = "localhost";
int port = 80;
InetAddress addr = InetAddress.getByName(hostname);
Socket socket = new Socket(addr, port);
// Send header
String path = "/mmi/slate/index.php";
File theFile = new File("E:/Studium/Multi/Projekt/newzealand.jpg");
System.out.println ("size: " + (int) theFile.length());
DataInputStream fis = new DataInputStream(new BufferedInputStream(new
FileInputStream(theFile)));
byte[] theData = new byte[(int) theFile.length( )];
fis.readFully(theData);
fis.close();
DataOutputStream raw = new
DataOutputStream(socket.getOutputStream());
Writer wr = new OutputStreamWriter(raw);
String command =
"POST "+path+" HTTP/1.0\r\n"
+ "Content-type: multipart/form-data, boundary=mango\r\n"
+ "Content-length: " + ((int) theFile.length()) + "\r\n"
+ "\r\n"
+ "--mango\r\n"
+ "content-disposition: name=\"MAX_FILE_SIZE\"\r\n"
+ "\r\n"
+ "\r\n--mango\r\n"
+ "content-disposition: attachment; name=\"datafile\";
filename=\"test.jpg\"\r\n"
+ "Content-Type: image/jpeg\r\n"
+ "Content-Transfer-Encoding: binary\r\n"
+ "\r\n";
wr.write(command);
wr.flush();
raw.write(theData);
raw.flush( );
wr.write("\r\n--mango--\r\n");
wr.flush( );
BufferedReader rd = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
wr.close();
raw.close();
socket.close();
} catch (Exception e) { System.out.println(e.toString()); }
}
}
************************************************+
Thanks for any help!
Chris and Martin