473,401 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

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 6448
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Badri Narayanan | last post by:
I accept a few user inputs and a file from the user, post it to my ASP page. I'm able to receive the data as well as the file properly in Receiver.asp. In my receiver.asp, I need to process the...
2
by: Amoril | last post by:
I am currently developing an app that will post an XML file (1 to 100MB+ in size) to an outside vendor application via HTTPS Post (doing this in a vb.net windows app). The vendor will then at some...
1
by: Jason Ho | last post by:
Hi, I would like to send a jpg file to a HTTP server by POST request. But I am not using a browser to do this, I use a Windows Form instead. I know how to send typical request by POST with code...
6
by: jackfoust | last post by:
I'm trying to POST from a form a simple username and password onto another website. Firefox and Opera post the data and results are returned successfully. The site returns "missing user id and...
0
by: deacon57 | last post by:
FYI - If you are a computer scientist (or geek), this post may be for you. I wanted to log any search keywords, etc that are used on my site. I created a simple program that logs search terms...
4
by: Natalia | last post by:
Hello, I need to provide the ability to post file and some form elements via our website (asp.net) to the third party website (asp page). On http://aspalliance.com/236#Page4 - I found great...
10
by: eggie5 | last post by:
Is it possible to get a file without using a form post? I want to get the data (bytes) of a file, text or binary, and just save it to a variable. Similar to the post body of a form that has a...
8
by: BiT | last post by:
Hello, I'm working right now on project in vb.net 2005 for my company, i need the project to download file from the company web site. In order to get the file i have to give the site address...
6
by: Brybot | last post by:
I am trying to allow HTTP POST file uploads to my web service. Currently I have it working perfectly for a SOAP/XML request reading in a byte using MemoryStream/FileStream but I cannot figure out...
2
by: MDANH2002 | last post by:
Hi From VB.NET I want to simulate the POST request of the following HTML form <html> <title>HTTP Post Testing</title> <body> <form action=http://www.example.com/postdata ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.