关于上传文件
```java
@Override
public void updateFile(HttpServletRequest request, HttpServletResponse response) throws IOException {
PrintWriter writer = response.getWriter();
Scanner scanner = new Scanner(request.getInputStream()).useDelimiter("\\A");
String body = scanner.hasNext() ? scanner.next() : "";
JSONObject jsonObj = JSONObject.parseObject(body);
log.info("在线编辑器回调参数:{}", jsonObj);
if((int) jsonObj.get("status") == 2|| (int) jsonObj.get("status") == 6) {
String downloadUri = (String) jsonObj.get("url");
String id = jsonObj.getString("key");
String[] value = id.split("=");
FileDataEntity dataEntity = baseMapper.selectById(value[0]);
URL url = new URL(downloadUri);
java.net.HttpURLConnection connection = (java.net.HttpURLConnection) url.openConnection();
@Cleanup InputStream stream = connection.getInputStream();
String path = System.getProperty("user.dir");
/*
log.info(dataEntity.getName(),dataEntity.getType());
File savedFile = new File(path+"/template/"+dataEntity.getName());
try (FileOutputStream out = new FileOutputStream(savedFile)) {
int read;
final byte[] bytes = new byte[1024];
while ((read = stream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
}
*/
//将文件刷回文件存储服务中
// 创建fileItem
File file = new File(path + "/template/" + dataEntity.getId() +"-" + dataEntity.getName());
FileItem fileItem = new DiskFileItem(
"file", // 表单参数名
ContentType.APPLICATION_FORM_URLENCODED.getMimeType(), // 文件类型
false, // 是否为表单格式
file.getName(), // 文件名
10240, // 超过多少byte存在磁盘上
file
);
//将更新的文件流写入到缓存的文件中,然后提交给文件服务
@Cleanup OutputStream outputStream = fileItem.getOutputStream();
IOUtils.copy(stream, outputStream);
connection.disconnect();
log.info("更新的文件大小:" + fileItem.getSize()+"");
MultipartFile mfile = new CommonsMultipartFile(fileItem);
log.info(String.valueOf(mfile.getSize()),mfile.getBytes());
try {
attachmentApi.update("annex", dataEntity.getAttachId(), mfile);
} catch (DataException e) {
log.error("文件更新出错了", e);
}
}
writer.write("{\"error\":0}");
}