apache httpclient3 httpclient4 上傳文件名亂碼問題

httpclient3解決方式:寫一個FilePart實現

package com.wxl.app;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.PartSource;
import org.apache.commons.httpclient.util.EncodingUtil;

public class CustomFilePart extends FilePart {

	public CustomFilePart(String name, PartSource partSource) {
		super(name, partSource);
	}

	public CustomFilePart(String name, File file, String contentType, String charset) throws FileNotFoundException {
		super(name, file, contentType, charset);
	}

	public CustomFilePart(String name, File file) throws FileNotFoundException {
		super(name, file);
	}

	public CustomFilePart(String name, PartSource partSource, String contentType, String charset) {
		super(name, partSource, contentType, charset);
	}

	public CustomFilePart(String name, String fileName, File file, String contentType, String charset)
			throws FileNotFoundException {
		super(name, fileName, file, contentType, charset);
	}

	public CustomFilePart(String name, String fileName, File file) throws FileNotFoundException {
		super(name, fileName, file);
	}

	protected void sendDispositionHeader(OutputStream out) throws IOException {
		super.sendDispositionHeader(out);
		String filename = getSource().getFileName();
		if (filename != null) {
			out.write(EncodingUtil.getAsciiBytes(FILE_NAME));
			out.write(QUOTE_BYTES);
			//使用UTF-8編碼
			out.write(EncodingUtil.getBytes(filename, "UTF-8"));
			out.write(QUOTE_BYTES);
		}
	}
}

package com.wxl.app;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class ClientMultipartFormPost {
	public static void main(String[] args) throws FileNotFoundException {
		StringPart name = new StringPart("name", "張三", "UTF-8");

		//使用自定義的FilePart,防止文件名亂碼。
		FilePart file = new CustomFilePart("file", new File("c:/測試.txt"));

		PostMethod filePost = new PostMethod("http://localhost:8080" + "/j2ee/simple");
		filePost.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true);

		MultipartRequestEntity reqEntity = new MultipartRequestEntity(new Part[] { name, file }, filePost.getParams());
		filePost.setRequestEntity(reqEntity);
		HttpClient client = new HttpClient();
		client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);

		try {
			int statusCode = client.executeMethod(filePost);
			System.out.println(HttpStatus.getStatusText(statusCode));
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			filePost.releaseConnection();
		}

	}
}



httpclient4解決方式:以瀏覽器兼容模式執行

MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, Charset.forName("UTF-8"));

package com.wxl.app;

import java.io.File;
import java.nio.charset.Charset;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
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;
import org.apache.http.util.EntityUtils;

/**
 * Example how to use multipart/form encoded POST request.
 */
public class ClientMultipartFormPost {

	public static void main(String[] args) throws Exception {
		HttpClient httpclient = new DefaultHttpClient();
		try {
			HttpPost httppost = new HttpPost("http://localhost:8080" + "/j2ee/simple");

			FileBody file = new FileBody(new File("c:/測試.txt"), "application/octet-stream", "UTF-8");
			StringBody name = new StringBody("張三", Charset.forName("UTF-8"));

			//以瀏覽器兼容模式運行,防止文件名亂碼。
			MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, Charset
					.forName("UTF-8"));
			
			reqEntity.addPart("file", file);
			reqEntity.addPart("name", name);

			httppost.setEntity(reqEntity);

			System.out.println("executing request " + httppost.getRequestLine());
			HttpResponse response = httpclient.execute(httppost);
			HttpEntity resEntity = response.getEntity();

			System.out.println("----------------------------------------");
			System.out.println(response.getStatusLine());
			if (resEntity != null) {
				System.out.println("Response content length: " + resEntity.getContentLength());
			}
			EntityUtils.consume(resEntity);
		} finally {
			try {
				httpclient.getConnectionManager().shutdown();
			} catch (Exception ignore) {
			}
		}
	}

}


發佈了86 篇原創文章 · 獲贊 6 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章