SpringMVC使用HttpClient实现文件上传

时间:2023-01-17 14:38:49

HttpClient API地址:http://hc.apache.org/httpclient-3.x/apidocs/


HttpClient httpclient = new DefaultHttpClient();

int statusCode=0;


1、使用Get方式访问

HttpGet get =null;

HttpParams conParams = httpclient.getParams();

HttpConnectionParams.setConnectionTimeout(conParams, 60000);

HttpConnectionParams.setSoTimeout(conParams, 600000);

HttpResponse response=null;

try {

//服务器端响应地址

String targetURL="http://localhost/File/httpServerUpload.json?area=" + val + "&fileType=" + fileTpye + ".replace(" ", "");

get = new HttpGet(targetURL);

response = httpclient.execute(get);

int a = response.getStatusLine().getStatusCode();

System.out.println("a-------------------->" + a);

if (a == 200) {

HttpEntity resEntity = response.getEntity();

String result = EntityUtils.toString(resEntity, "UTF-8");// httpclient自带的工具类读取返回数据

System.out.println("result------------------------------------------>"+result);

EntityUtils.consume(resEntity);

if(result.equals("200")){

return ResponseData.SUCCESS_NO_DATA;

}else{

return ResponseData.FAILED_NO_DATA;

}

} else {

return new ResponseData(false, "服务器响应失败");

}

} catch (Exception e) {

   e.printStackTrace();

}



2、使用Post访问

try{

//服务器端响应地址

String targetURL="http://localhost/File/httpServerUpload.json?area=" + val + "&fileType=" + fileTpye + ".replace(" ", "");

HttpPost httppost = new HttpPost(targetURL);

FileBody fileBody = new FileBody(uploadFile);

MultipartEntity reqEntity = new MultipartEntity();

//传递参数

reqEntity.addPart("fileBody", fileBody);

httppost.setEntity(reqEntity);

HttpResponse responseBack = httpclient.execute(httppost);

//响应返回值

statusCode = responseBack.getStatusLine().getStatusCode();

System.out.println("statusCode--------------------------------->"+statusCode);

if (statusCode == HttpStatus.SC_OK) {

HttpEntity resEntity = responseBack.getEntity();

String result = EntityUtils.toString(resEntity, "UTF-8");// httpclient自带的工具类读取返回数据

System.out.println("result------------------------------------------>"+result);

EntityUtils.consume(resEntity);


if(result.equals("200")){

return ResponseData.SUCCESS_NO_DATA;

}else{

return ResponseData.FAILED_NO_DATA;

}

} else {

return new ResponseData(false, "服务器响应失败");

}

}catch(Exception e){

e.printStackTrace();

}



3、服务器端响应代码 采用springMVC get方式不能使用以下方式上传 必须POST 并且from表单需要设置enctype="multipart/form-data" 返回值ResponseData可根据项目要求修改

// httpClient方式文件上传 --服务端

@RequestMapping(value = "/httpServerUpload.json")

@ResponseBody

public ResponseData httpServerUpload(@RequestParam("fileBody") MultipartFile fileBody,

HttpServletRequest request, HttpServletResponse response) throws IOException {

System.out.println("---------------------------服务端----------------------------------");

response.reset();

PrintWriter out = response.getWriter();

// 响应客户端

response.setContentType("text/html");

try {

String val = request.getParameter("area");//获取文件保存地址

String fileType = request.getParameter("fileType");//获取文件类型

//判断上传文件是否存在

if(fileBody.isEmpty()){

out.write("404");

return null;

}

//判断保存路径是否存在

File savePathFile = new File(val);

if(!savePathFile.exists() && !savePathFile.isDirectory()){

out.write("404");

return null;

}

//判断文件类型是否为空

if(fileType.isEmpty() || "".equals(fileType)){

out.write("500");

return null;

}

String path = val + "123456" +".JPG";

byte[] fileByte = fileBody.getBytes();

FileUtils.writeByteArrayToFile(new File(path), fileByte);

out.write("200");

} catch (Exception e) {

e.printStackTrace();

}finally {

out.close();

}

return ResponseData.SUCCESS_NO_DATA;

}