四、使用HttpClient上传测试文件服务器

直接上代码吧!

package cn.java.basic.upload;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
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.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;

public class HttpUpload {
	private static final String UPLOADURL = "http://192.168.0.181:8099/upload";
	private static final String KEY_USERNAME = "username";
	private static final String KEY_MD5ORSHA1 = "md5orsha1";
	private static final String KEY_FILE = "myfile";

	private String username;
	private String md5orsha1;
	private String filePath;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getMd5orsha1() {
		return md5orsha1;
	}

	public void setMd5orsha1(String md5orsha1) {
		this.md5orsha1 = md5orsha1;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public HttpUpload(String username, String md5orsha1, String filePath) {
		super();
		this.username = username;
		this.md5orsha1 = md5orsha1;
		this.filePath = filePath;
	}

	public String postFile() throws ClientProtocolException, IOException {
		FileBody fileBody = null;
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(UPLOADURL);
		File file = new File(filePath);
		if (file != null) {
			fileBody = new FileBody(file);
		}
		StringBody usernameStringBody = new StringBody(username);
		StringBody md5orsha1StringBody = new StringBody(md5orsha1);
		MultipartEntity reqEntity = new MultipartEntity();
		reqEntity.addPart(KEY_USERNAME, usernameStringBody);
		reqEntity.addPart(KEY_MD5ORSHA1, md5orsha1StringBody);
		reqEntity.addPart(KEY_FILE, fileBody);
		httppost.setEntity(reqEntity);
		System.out.println("执行: " + httppost.getRequestLine());
		HttpResponse response = httpclient.execute(httppost);
		System.out.println("statusCode is "
				+ response.getStatusLine().getStatusCode());
		HttpEntity resEntity = response.getEntity();
		System.out.println("----------------------------------------");
		System.out.println(response.getStatusLine());
		if (resEntity != null) {
			System.out.println("返回长度: " + resEntity.getContentLength());
			System.out.println("返回类型: " + resEntity.getContentType());
			InputStream in = resEntity.getContent();
			String returnValue = inputStream2String(in);
			System.out.println("returnValue:" + returnValue);
			return returnValue;
		}
		if (resEntity != null) {
			resEntity.consumeContent();
		}
		return null;
	}

	public static String inputStream2String(InputStream in) throws IOException {
		StringBuffer out = new StringBuffer();
		byte[] b = new byte[4096];
		for (int n; (n = in.read(b)) != -1;) {
			out.append(new String(b, 0, n));
		}
		return out.toString();
	}

	public static void main(String[] args) throws ClientProtocolException,
			IOException {
		String username = "10000";
		String md5orsha1 = "11111111111111111111111111111112";
		String filePath = "e:\\b.jpg";
		HttpUpload hu = new HttpUpload(username, md5orsha1, filePath);
		System.out.println(hu.postFile());
	}
}

 

 

输出结果:


 

使用返回的地址去访问图片


工程需要如下包:

 


附上jar包吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值