应用场景:
java代码部分做文件资源同步。比如:应用系统将上传的文件备份或同步至文件服务器中。
代码位置
位于java client相关代码部分,将文件流通过httpClient工具发送网络请求到服务器上。
关键代码
将java.io.File对象添加到HttpEntity(org.apache.http.HttpEntity)对象中:
MultipartEntityBuilder mEntityBuilder = MultipartEntityBuilder.create();
mEntityBuilder.addBinaryBody("file", file);
httppost.setEntity(mEntityBuilder.build());
Client完整代码
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.ParseException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class FileClient {
/**
* 将文件提交至文件服务器
* @param file 文件对象
* @return FileStatus 上传结果
*/
public static String postFile(File file) {
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String result = null;
try {
HttpPost httpPost = new HttpPost(ServerURL.URL_FILE_POST);
MultipartEntityBuilder mEntityBuilder = MultipartEntityBuilder.create();
mEntityBuilder.addBinaryBody("file", file);
httpPost.setEntity(mEntityBuilder.build());
response = httpclient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity resEntity = response.getEntity();
result =EntityUtils.toString(resEntity);
// 消耗掉response
EntityUtils.consume(resEntity);
}
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
HttpClientUtils.closeQuietly(httpclient);
HttpClientUtils.closeQuietly(response);
}
return result;
}
}
Server代码
/**
* 上传文件
* @param fileRequest
*/
@RequestMapping("/postFile")
public void postFile(HttpServletRequest request, HttpServletResponse response) {
String result = "error";
Map<String, Object> map = new HashMap<String, Object>();
// 创建一个通用的多部分解析器
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession()
.getServletContext());
// 判断 request 是否有文件上传,即多部分请求
if (multipartResolver.isMultipart(request)) {
// 转换成多部分request
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
// 取得request中的所有文件名
Iterator<String> iter = multiRequest.getFileNames();
while (iter.hasNext()) {
// 取得上传文件
MultipartFile multipartFile = multiRequest.getFile(iter.next());
if (multipartFile != null) {
// 取得当前上传文件的文件名称
String fileName = multipartFile.getOriginalFilename();
if (fileName.trim() != null && fileName.trim().length() > 0) {
CommonsMultipartFile cf = (CommonsMultipartFile) multipartFile;
DiskFileItem fi = (DiskFileItem) cf.getFileItem();
File tempFile = fi.getStoreLocation();
// 拿到文件,存储
...
result = "success";
}
}
}
}
}
map.put("result", result);
this.renderJson(response, map);
}