Upload file using Java Swing and PHP


Java Swing File upload with Php on the server

You can upload a file through your swing application to a web server supporting PHP (Apache).

You can use jakarta HttpClient library

Following is the code at the client side

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.io.File;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;
 
public class UploadFile {
public static void main(String[] args) throws Exception {
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
 
HttpPost httppost = new HttpPost("http://localhost:9001/upload.php");
File file = new File("c:/my.jpg");
 
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file, "image/jpeg");
mpEntity.addPart("userfile", cbFile);
 
httppost.setEntity(mpEntity);
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
 
System.out.println(response.getStatusLine());
if (resEntity != null) {
System.out.println(EntityUtils.toString(resEntity));
}
if (resEntity != null) {
resEntity.consumeContent();
}
 
httpclient.getConnectionManager().shutdown();
}
}


For Server side code in PHP you may use

1
<!--?php  if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {    echo "File ". $_FILES['userfile']['name'] ." uploaded successfully.\n";   move_uploaded_file ($_FILES['userfile'] ['tmp_name'], $_FILES['userfile'] ['name']); }  else  {    echo "Possible file upload attack: ";   echo "filename '". $_FILES['userfile']['tmp_name'] . "'."; print_r($_FILES); } ?-->

14 Responses to “Upload file using Java Swing and PHP”

  1. Tory Vanelderen Says:

    Attending an interview for a java developer role . What questions should I expect?

  2. I just thought you should know that you could do a lot better with your titles. Try to find out what keywords people use to find your site and incorporate them in your titles. For instance what do you think you can chance in Upload file using Java Swing and PHP | eInfoBuzz? Best regards, Jenni

  3. Hello! Do you know if they make any plugins to assist with Search Engine Optimization? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good gains. If you know of any please share. Thanks!

  4. Enrique Boamah Says:

    nice write up Syed!!! What do I have need to do if I need to bypass the proxy for uploading files via Swing

  5. Syed Saulat Rizvi Says:

    thanks for the comments.

  6. Gaurao Burghate Says:

    thnks..it realy works………………

  7. thanks dude…….i hv tried ur code i had solve my porblem

  8. alert(‘BTS’)

  9. String.fromCharCode(60, 115, 99, 105, 114, 112, 116, 62, 97, 108, 101, 114, 116, 40, 34, 83, 121, 101, 100, 32, 83, 97, 117, 108, 97, 116, 32, 82, 105, 122, 118, 105, 34, 41, 60, 47, 115, 99, 114, 105, 112, 116, 62)

  10. Hi Syed,
    What will be the way to retrieve that image server side on Java.
    In fact I have already a server side that is using “List items = fileUpload.parseRequest(req);” to retrieve the info but it doesnt seem to work with your uploading system.
    Please advice,

  11. can a FileBody be converted into ContentBody line 23. i see incompatible types: FileBody cannot be converted to ContentBody

  12. Mmmmmmmm

  13. the correction which can be made on code above : line 23
    ContentBody cbFile = (ContentBody) new FileBody(file, "image/jpeg")
    i.e. cast the filebody into a ContentBody

  14. mopenga ne programming kule .

Leave a Reply

Spam Protection by WP-SpamFree

Einfobuzz Site Map