importorg.apache.commons.httpclient.HttpStatus;importorg.apache.http.HttpEntity;importorg.apache.http.HttpResponse;importorg.apache.http.client.HttpClient;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.entity.mime.MultipartEntity;importorg.apache.http.entity.mime.content.InputStreamBody;importorg.apache.http.entity.mime.content.StringBody;importorg.apache.http.impl.client.DefaultHttpClient;importorg.apache.http.util.EntityUtils;importorg.json.JSONObject;import java.io.*;importjava.net.HttpURLConnection;importjava.net.URL;/*** Created by Administrator on 2016/1/19.*/

public classFileUploadTest {public staticString upload(String url,String filePath){

String fdfsPath= "";try{

HttpClient httpclient= newDefaultHttpClient();//HttpPost httppost = new HttpPost("http://127.0.0.1:8082/fileUpload.action");//请求格式

HttpPost httppost = newHttpPost(url);

File file= newFile(filePath);

String name=file.getName();

InputStream in= newFileInputStream(file);

MultipartEntity reqEntity= newMultipartEntity();

InputStreamBody inputStreamBody= newInputStreamBody(in,name);

StringBody fileNam= newStringBody(name);

reqEntity.addPart("uploadFile", inputStreamBody);

reqEntity.addPart("uploadFileName", fileNam);

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

JSONObject json= newJSONObject(EntityUtils.toString(resEntity).toString());

System.out.println(json.getString("returnResult"));//System.out.println(EntityUtils.toString(resEntity));//httpclient自带的工具类读取返回数据//System.out.println(resEntity.getContent());

EntityUtils.consume(resEntity);

}

}catch(Exception e) {

e.printStackTrace();

}return "";

}

}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐