472,807 Members | 3,226 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,807 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 6416
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 ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.