PostFileToServer

Aus Programmers Guide

(Unterschied zwischen Versionen)
Wechseln zu: Navigation, Suche
K
K
 
Zeile 16: Zeile 16:
public class PostFile {
public class PostFile {
private static String serverIP = "<ip>";
private static String serverIP = "<ip>";
 +
private static String postName = "<name_der_upload_datei>";
public static void main(String[] args) throws Exception {
public static void main(String[] args) throws Exception {
Zeile 48: Zeile 49:
MultipartEntity mpEntity = new MultipartEntity();
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file, "text/plain");
ContentBody cbFile = new FileBody(file, "text/plain");
-
mpEntity.addPart("<post_name>", cbFile);
+
mpEntity.addPart(postName , cbFile);
httppost.setEntity(mpEntity);
httppost.setEntity(mpEntity);

Aktuelle Version vom 15:16, 5. Mai 2010

Mit Java eine Datei hochladen

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 PostFile {
	private static String serverIP = "<ip>";
	private static String postName = "<name_der_upload_datei>";
 
	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://" + serverIP
				+ "/uploader.php");
		File file = null;
 
		if (args.length != 0) {
			file = new File(args[0]);
		} else {
			System.out.println("Keine Datei übergeben!");
			System.exit(-1);
		}
		if (!file.isFile()) {
			System.err.println("Die Datei existiert nicht!");
			JOptionPane.showMessageDialog(null,
					"Die Datei existiert nicht! :(", "Fehler",
					JOptionPane.ERROR_MESSAGE);
			System.exit(-1);
		}
		if (!file.canRead()) {
			System.err.println("Ich darf die Datei nicht lesen");
			JOptionPane.showMessageDialog(null,
					"Ich darf die Datei nicht lesen :(", "Fehler",
					JOptionPane.ERROR_MESSAGE);
			System.exit(-1);
		}
		MultipartEntity mpEntity = new MultipartEntity();
		ContentBody cbFile = new FileBody(file, "text/plain");
		mpEntity.addPart(postName , 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();
 
	}
Persönliche Werkzeuge