469,283 Members | 2,349 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,283 developers. It's quick & easy.

post file to php

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
Jul 17 '05 #1
4 6103
Christian,

Content-Length header value in case of "multipart/form-data" content
type should include everything including part header and boundaries.
That's where you're loosing 199 bytes.

Best regards,
Igor.

Christian Galbavy пишет:
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

Jul 17 '05 #2
Hello!

Ok, I have got this. But what is my right content-length?
For example, this is my header and the file:
File theFile = "E:/Studium/Multi/my_picture.jpg"
**************************************
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=\"" + theFile.getName() + "\"\r\n"
+ "Content-Type: image/jpeg\r\n"
+ "Content-Transfer-Encoding: binary\r\n"
+ "\r\n";
*************************************

What is the right length in this case (theFile.length() + ??), and how can I
calculate it?
I really appreciate your help.
Thank you
Christian Galbavy

"Igor Kolomiyets" <li**@inksystems.net> schrieb im Newsbeitrag
news:_v*******************@news.indigo.ie...
Christian,

Content-Length header value in case of "multipart/form-data" content
type should include everything including part header and boundaries.
That's where you're loosing 199 bytes.

Best regards,
Igor.


Jul 17 '05 #3
Christian,

i would do it this way.

First split your command variable into two and note the length of the
trailing boundary marker:
String header = "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";

String command = "--mango\r\n"
+ "content-disposition: name=\"MAX_FILE_SIZE\"\r\n"
+ "\r\n"
+ "\r\n--mango\r\n"
+ "content-disposition: attachment; name=\"datafile\";
filename=\""
+ theFile.getName() + "\"\r\n"
+ "Content-Type: image/jpeg\r\n"
+ "Content-Transfer-Encoding: binary\r\n"
+ "\r\n";

String trail = "\r\n--mango--\r\n";

The right length in this case will be:

theFile.length() + command.length() + trail.length()
Best regards,
Igor.

Christian Galbavy пишет:
Hello!

Ok, I have got this. But what is my right content-length?
For example, this is my header and the file:
File theFile = "E:/Studium/Multi/my_picture.jpg"
**************************************
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=\"" + theFile.getName() + "\"\r\n"
+ "Content-Type: image/jpeg\r\n"
+ "Content-Transfer-Encoding: binary\r\n"
+ "\r\n";
*************************************

What is the right length in this case (theFile.length() + ??), and how can I
calculate it?
I really appreciate your help.
Thank you
Christian Galbavy

"Igor Kolomiyets" <li**@inksystems.net> schrieb im Newsbeitrag
news:_v*******************@news.indigo.ie...
Christian,

Content-Length header value in case of "multipart/form-data" content
type should include everything including part header and boundaries.
That's where you're loosing 199 bytes.

Best regards,
Igor.


Jul 17 '05 #4
Thanks a lot!
Now I understand it, and it works.
Christian
"Igor Kolomiyets" <li**@inksystems.net> schrieb im Newsbeitrag
news:Rm*******************@news.indigo.ie...
Christian,

i would do it this way.

First split your command variable into two and note the length of the
trailing boundary marker:
String header = "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";

String command = "--mango\r\n"
+ "content-disposition: name=\"MAX_FILE_SIZE\"\r\n"
+ "\r\n"
+ "\r\n--mango\r\n"
+ "content-disposition: attachment; name=\"datafile\";
filename=\""
+ theFile.getName() + "\"\r\n"
+ "Content-Type: image/jpeg\r\n"
+ "Content-Transfer-Encoding: binary\r\n"
+ "\r\n";

String trail = "\r\n--mango--\r\n";

The right length in this case will be:

theFile.length() + command.length() + trail.length()
Best regards,
Igor.

Christian Galbavy пишет:
Hello!

Ok, I have got this. But what is my right content-length?
For example, this is my header and the file:
File theFile = "E:/Studium/Multi/my_picture.jpg"
**************************************
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=\"" + theFile.getName() + "\"\r\n"
+ "Content-Type: image/jpeg\r\n"
+ "Content-Transfer-Encoding: binary\r\n"
+ "\r\n";
*************************************

What is the right length in this case (theFile.length() + ??), and how can I calculate it?
I really appreciate your help.
Thank you
Christian Galbavy

"Igor Kolomiyets" <li**@inksystems.net> schrieb im Newsbeitrag
news:_v*******************@news.indigo.ie...
Christian,

Content-Length header value in case of "multipart/form-data" content
type should include everything including part header and boundaries.
That's where you're loosing 199 bytes.

Best regards,
Igor.


Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Amoril | last post: by
6 posts views Thread by jackfoust | last post: by
reply views Thread by deacon57 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.