HttpClient4.2.1上传文件,中文文件名乱码

花谢尊前不敢香 2012-10-18 03:44:07
HttpClient4.2.1上传文件,中文名乱码问题,如何解决,有过相同问题的给个解决方法,网上的方法试过了……
...全文
775 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jonathan-Wei 2013-07-14
  • 打赏
  • 举报
回复
能不能把完整的工程发我一份,谢谢!252637867@qq.com
Dven 2013-06-14
  • 打赏
  • 举报
回复
请问是修改jar包的哪几个部分 谢谢
dengjierb 2013-05-27
  • 打赏
  • 举报
回复
问下 怎么改的??
  • 打赏
  • 举报
回复
已重新修改了jar文件,编译后可以传输中文了。
  • 打赏
  • 举报
回复
public ActionForward hdytest(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ProjectForm proFrom = (ProjectForm) form;
String name = request.getParameter("name");
String account = request.getParameter("account");
String fileName = request.getParameter("fileName");
String descript = request.getParameter("descript");
String myFile = request.getParameter("file");
FormFile file = proFrom.getFile();
System.out.println();

try {
String fileName0 = new String(file.getFileName().getBytes(),
"ISO-8859-1");
String fileName1 = new String(file.getFileName().getBytes("UTF-8"),
"ISO-8859-1");
String fileName2 = new String(file.getFileName().getBytes("GBK"),
"ISO-8859-1");
String fileName4 = new String(file.getFileName().getBytes(
"ISO-8859-1"), "UTF-8");
String fileName5 = new String(file.getFileName().getBytes(
"ISO-8859-1"), "GBK");
String fileName6 = new String(file.getFileName().getBytes(
"ISO-8859-1"));
String fileName7 = new String(file.getFileName().getBytes("GBK"));
String fileName8 = new String(file.getFileName().getBytes("UTF-8"));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

FileOutputStream fileOutput;

try {
fileOutput = new FileOutputStream("d://hdytest//"
+ new String(file.getFileName().getBytes(), "gb2312"));
fileOutput.write(file.getFileData());
fileOutput.flush();
fileOutput.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

response.setContentType("text/text;charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
String xml = " <person><xingming>zhangsan</xingming><age>18</age></person>";
try {
response.getWriter().print(xml);
} catch (IOException e) {
e.printStackTrace();
}
return null;

}
  • 打赏
  • 举报
回复

package com.http;

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

import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
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.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 HttpUploadFile {

public static void main(String[] args) {
HttpClient client = new DefaultHttpClient();
// client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
// client.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");
client.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, Charset.forName("UTF-8"));
HttpPost post = new HttpPost("http://localhost:8080/app/hdytest.do?method=hdytest");
// post.setHeader("Charset", "utf-8");
// post.setHeader("contentType", "application/octet-stream;charset=ISO-8859-1");
File file = new File("d:\\我.txt");

// post.setEntity(new UrlEncodedFormEntity(nameValue,"utf-8"));

MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file);
mpEntity.addPart("file", cbFile);


post.setEntity(mpEntity);
HttpResponse response;
String content = null;
try {
response = client.execute(post);
content = EntityUtils.toString(response.getEntity());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(content);
client.getConnectionManager().shutdown();

}
}


znnevergiveup 2012-10-18
  • 打赏
  • 举报
回复
字符集 UTF-8

好吧 我酱油~~~

67,516

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧