-----客户端
import java.io.File;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.ParseException;
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;
import org.apache.http.util.EntityUtils;
public static void submitPost(String url,String filename1,String filename2, String filepath){
HttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httppost = new HttpPost(url);
FileBody bin = new FileBody(new File(filepath + File.separator + filename1));
System.out.println("Filename:"+ bin.getFilename());;
// FileBody bin2 = new FileBody(new File(filepath + File.separator + filename2));
StringBody comment = new StringBody(filename1);
StringBody commentType = new StringBody("1");
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("filename", commentType);//传单个参数
reqEntity.addPart("file", bin);//file1为请求后台的File upload;属性
// reqEntity.addPart("file2", bin2);//file2为请求后台的File upload;属性
// reqEntity.addPart("filename1", comment);//filename1为请求后台的普通参数;属性
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
int statusCode = response.getStatusLine().getStatusCode();
if(statusCode == HttpStatus.SC_OK){
System.out.println("服务器正常响应.....");
HttpEntity resEntity = response.getEntity();
System.out.println(EntityUtils.toString(resEntity));//httpclient自带的工具类读取返回数据
System.out.println(resEntity.getContent());
EntityUtils.consume(resEntity);
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
httpclient.getConnectionManager().shutdown();
} catch (Exception ignore) {
}
}
}
-----服务端:(用的struts2方式)
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;
public void uploadfile() throws IOException {
String jsonStr="";
try {
if(request instanceof MultiPartRequestWrapper){
MultiPartRequestWrapper pr = (MultiPartRequestWrapper) request;
if(pr.getFiles("file")!=null)
{
File file = pr.getFiles("file")[0];
String filename =request.getParameter("filename");
String dir = dirMap.get("3");
String path = request.getSession().getServletContext().getRealPath(dir);
// String uuid=UUID.randomUUID().toString().replaceAll("-", "");
File f = new File(path);
if(!f.exists()){
f.mkdirs();
}
File des = new File(path+File.separator+filename);
FileUtils.copyFile(file, des);
Message mo = new Message();
jsonStr = "{\"rescode\":\"0000\",\"filepath\":"+dir+File.separator+filename+"}";//文件上传成功
}
}else{
jsonStr = "{\"rescode\":\"0001\"}";//文件上传失败
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
jsonStr = "{\"rescode\":\"9999\"}";//文件上传失败
}
response.getWriter().print(jsonStr);
}