Did anyone upload file to php server by using swing applet before? Need help!!!

H

hu.brett

Hi,

I need a help. Recently I wrote a swing applet for uploading files to
php server. It seems that everything is fine now. I can get the exactly
same size files in the server. For text file, there is no any problem
to be openned. But for image and moive files (such as .jpeg, .gif .mov
and etc.), those files can not be openned after they are uploaded to
sever even the size is exactly the same as orignal files.
I think that it should be caused by "Content-Type".
For testing, I change my Content-Type to "image/gif" to send gif files.
However, the problem is still there. Now, I have no idea about this
problem. Could any one give me a favor? Thanks in advance!!!

I attached the code related to connection and sending data here for
your review.

public boolean upload() throws Exception{
boolean done = false;
String file_name = file.getName();
String file_data = readFile();

StringBuffer response = new StringBuffer("");

OutputStream os = null;
BufferedReader in = null;
HttpURLConnection conn = null;

try{
URL serverURL = new URL(url);
// connect to server
URLConnection uc = serverURL.openConnection();
conn = (HttpURLConnection) uc;
conn.setAllowUserInteraction(true);
conn.setInstanceFollowRedirects(true);
// set connection as POST
conn.setRequestMethod("POST");

conn.setDoOutput(true); // turns it into a post

// setup headers
conn.setRequestProperty(
"Content-Type",
"multipart/form-data; boundary=" + CONTENT_BOUNDARY);

conn.setRequestProperty("Accept-Language", "en-us");
conn.setRequestProperty("Accept-Encoding", "gzip, deflate");

conn.setRequestProperty("CACHE-CONTROL", "no-cache");

os = conn.getOutputStream();


String request =
"--"
+ CONTENT_BOUNDARY
+ "\r\n"
+ "Content-Disposition: form-data; name=\"upfile\"\r\n\r\n"
+ file_name
+ "\r\n"
+ "--"
+ CONTENT_BOUNDARY
+ "\r\n"
+ "Content-Disposition: form-data; name=\"upfile\"; filename=" +
file_name
+ "\r\nContent-Type: multipart/form-data\r\n\r\n"
+ file_data //file is read into a string here, is it OK? But no
other choose.
+ "\r\n"
+ "--"
+ CONTENT_BOUNDARY
+ "\r\n";

System.out.println("DEBUG: Sending the following request:\n\r" +
request);
System.out.println("DEBUG: Sending the post request...\n\r");
os.flush();

os.write(request.getBytes(), 0, request.getBytes().length);
os.flush();

in = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
String inputLine;

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}

// closing connections
in.close();
in = null;
os.close();
os = null;
conn.disconnect();
conn = null;

done = true;
 
A

Andrew Thompson

...Recently I wrote a swing applet
*

..for uploading files to
php server. It seems that everything is fine now.

Folks have some odd definitions of 'fine'.
...I can get the exactly
same size files in the server. For text file, there is no any problem
to be openned. But for image and moive files (such as .jpeg, .gif .mov
and etc.), those files can not be openned after they are uploaded to
sever even the size is exactly the same as orignal files.

It does sound as if all uploads are arriving as type text
rather than binary from that snippet of information. But..
I think that it should be caused by "Content-Type".
For testing, I change my Content-Type to "image/gif" to send gif files.
However, the problem is still there.

...I am not sure the way to fix it.

Others more expereienced with uploads might be
able to spot the problem, but if you prepare an SSCCE**
I will investigate further.

** SSCCE <http://www.physci.org/codes/sscce>
* Oh.. but strip the GUI from it and make it a command line app.,
no need for a GUI to see this break.

Andrew T.
 
H

hu.brett

Hi, I solved the problem already. The key point is that we can not use
stream to read and upload a image file. We should use byte[] to read
and upload a image file. I got a little bit sense about the difference
between them, but not very clear. If anyone can give me a detailed
information about this problem, I appreciate your help. Thanks in
advance.

BTW, if I upload file by using a java applet to a servlet, should I
still care about this? Because I used stream before and did not get
any trouble. However I normally did something in servlet.
 
O

Oliver Wong

Hi, I solved the problem already. The key point is that we can not use
stream to read and upload a image file. We should use byte[] to read
and upload a image file. I got a little bit sense about the difference
between them, but not very clear. If anyone can give me a detailed
information about this problem, I appreciate your help. Thanks in
advance.

I'm guessing you read the files as characters, instead of bytes, and
tried to transfer the characters across instead of the bytes.

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top