http post 上传文件

-----客户端 

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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值