Did anyone upload file to php server by using swing applet before? Need help!!!  
Author Message
hu.brett





PostPosted: 2006-12-5 6:52:00 Top

java-programmer, Did anyone upload file to php server by using swing applet before? Need help!!! 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;

 
Andrew Thompson





PostPosted: 2006-12-5 10:31:00 Top

java-programmer >> Did anyone upload file to php server by using swing applet before? Need help!!! email***@***.com wrote:
...
> ...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.

 
hu.brett





PostPosted: 2006-12-6 1:41:00 Top

java-programmer >> Did anyone upload file to php server by using swing applet before? Need help!!! 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.

On Dec 4, 9:30 pm, "Andrew Thompson" <email***@***.com> wrote:
> email***@***.com wrote:...
>
> > ...Recently I wrote aswingapplet*
>
> > ..for uploading files to
> >phpserver. It seems that everything is fine now.Folks have some odd definitions of 'fine'.
>
> >...I can get the exactly
> > same size files in theserver. For textfile, 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.,
> noneedfor a GUI to see this break.
>
> Andrew T.

 
 
Oliver Wong





PostPosted: 2006-12-6 5:02:00 Top

java-programmer >> Did anyone upload file to php server by using swing applet before? Need help!!! <email***@***.com> wrote in message
news:email***@***.com...
> 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