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;

.



Relevant Pages

  • Re: bug in windows with adodb
    ... > I'm uploading a file to a server using ADODB.Stream in JavaSript, ... Convert the string to Base64 in Javascript. ... You will need some server side scripting to access from then on. ...
    (microsoft.public.scripting.jscript)
  • Nortel Contact Recording Centralized Archive 6.5.1 EyrAPIConfiguration getSubKeys() Remote SQL Inje
    ... Web Service getSubKeys() Remote SQL Injection Exploit ... Microsoft SQL Server 2005 Express ... public String getSubKeys ... Connection conn; ...
    (Bugtraq)
  • Re: Encrypted Connection String and Security....Quick Question
    ... Why don't you put the encrypted string straight into the web.config before ... I then encrypt the connection string using ... I upload the site to the shared hosting server. ...
    (microsoft.public.dotnet.framework.aspnet)
  • SQL Connection Problem
    ... An error has occurred while establishing a connection to the server. ... database location within the applications App_Data directory. ... Boolean& failoverDemandDone, String host, String failoverPartner, String ... user, String password, Boolean trusted, String connectionString) +68 ...
    (microsoft.public.dotnet.framework.aspnet)
  • server-side JavaScript: Prototypes of built-in classes, objects and functins
    ... Session object (disk-based session variables for data persistence ... File class (manipulation of files on server, ie. open, close, read, ... //Methods Cgi.queryCgi.postCgi.anyby default return an empty string if requested var not found ...
    (comp.lang.javascript)