客户端代码:
/**
* post向服务器上传文件的方法
* @param url 服务器端接收文件地址
* @param sourceFilePath 源文件的绝对路径
* @param targetRelativePath 目标文件的相对路径
* @return
*/
public static boolean postImageMethod(String url,String sourceFilePath,String targetRelativePath)
{
// 创建默认的httpClient实例
CloseableHttpClient httpclient = HttpClients.createDefault();
// 创建httppost
HttpPost httppost = new HttpPost(url);
try {
FileBody file = new FileBody(new File(sourceFilePath));
StringBody filePath = new StringBody(targetRelativePath);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("file",file);
reqEntity.addPart("relativePath",filePath);
httppost.setEntity(reqEntity);
CloseableHttpResponse response = httpclient.execute(httppost);
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
if(EntityUtils.toString(entity, "UTF-8").equals("{\"returnCode\":0,\"returnMsg\":\"success\",\"sendResult\":1}")){
return true;
}
}
EntityUtils.consume(entity);
}finally{
response.close();
}
}catch(Exception e){
e.printStackTrace();
} finally{
// 关闭连接,释放资源
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
服务器端代码:
/**
* 接受服务产品图像文件
* @param response
* @param getResultSet
*/
@RequestMapping(value = "acceptVisFile")
public void syncVisDataFile(HttpServletResponse response,HttpServletRequest request) throws IOException {
System.out.println("+++++++++接受服务产品图像的接口被调用!!!");
request.setCharacterEncoding("UTF-8");
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
System.out.println("isMultipart:"+isMultipart);
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
//得到所有的文件
List<FileItem> items = upload.parseRequest(request);
Iterator<FileItem> iter = items.iterator();
FileItem item = null;
String paramName = null;
String paramValue = null;
String fieldName = null;
String fileName = null;
InputStream in = null;
while (iter.hasNext()) {
item = (FileItem) iter.next();
System.out.println("item:"+item);
if (item.isFormField()) {
//普通文本信息处理
paramName = item.getFieldName();
paramValue = item.getString("UTF-8");
System.out.println("普通文本处理信息:"+"paramName: "+ paramName);
System.out.println("普通文本处理信息:"+"paramValue:"+ paramValue);
} else {
//上传文件信息处理
fieldName = item.getFieldName();
fileName = item.getName();
System.out.println("上传文件信息处理:"+"fieldName:"+fieldName);
System.out.println("上传文件信息处理:"+"fileName:"+fileName);
in = item.getInputStream();
}
}
//服务产品文件存储的相对路径(需要在数据库配置?)
String realativePath = "/space/visCloud/"+paramValue;
//服务产品文件的存储路径
File storageDir = new File(realativePath);
if(!storageDir.exists()){ //如果路径不存在,则创建目录
storageDir.mkdirs();
}
String filePath = realativePath+"/"+ fileName;
System.out.println("服务产品文件存储的最终路径为:"+filePath);
FileOutputStream fos = new FileOutputStream(filePath);
byte[] buffer = new byte[1024];
while (in.read(buffer) > 0) {
fos.write(buffer, 0, buffer.length);
}
fos.flush();
fos.close();
} catch (FileUploadException e) {
e.printStackTrace();
}
StringBuffer str = new StringBuffer("{\"returnCode\":0,\"returnMsg\":\"success\",\"sendResult\":1}");
renderText(str.toString(), response);
}