67,516
社区成员
发帖
与我相关
我的任务
分享
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();
}
}