@ResponseBody
@RequestMapping("/upload")
public String upload(HttpServletRequest request, @RequestParam(value = "uploadfile") MultipartFile file,
@RequestParam(value = "policyno", required = false)String policyno) {
AjaxResult Result = new AjaxResult();
Result.setStatus(HttpStatus.SC_OK);
FileUpload fileUpload = new FileUpload();
fileUpload.setPolicyno(policyno);
int count = fileUploadService.queryCount(fileUpload);
if (count >= 5) {
Result.setStatusText("上传文件过多:只能上传5个的文件");
return JSON.toJSONString(Result);
}
InputStream is = null;
String name = file.getOriginalFilename();
if (!file.isEmpty() && file.getSize() > 0) {
System.out.println("文件长度: " + file.getSize());
System.out.println("文件类型: " + file.getContentType());
System.out.println("文件名称: " + file.getName());
System.out.println("文件原名: " + name);
System.out.println("========================================");
if (file.getSize() > 1024 * 1024 * 2) {
Result.setStatus(HttpStatus.SC_OK);
Result.setStatusText("上传失败:只能上传小于 2 M 的文件");
Result.setData(name);
return JSON.toJSONString(Result);
}
try {
is = file.getInputStream();
byte[] fileByte = toByteArray(is);
FileUpload fileU = new FileUpload();
fileU.setAttachment(fileByte);//该字段对应的数据库类型为mediumblob,最大4M
fileU.setFilename(name);
fileU.setInsertdate(new Date());
fileU.setUpdatedate(new Date());
fileU.setDeleted("false");
fileU.setType("claim");
fileU.setContentType(file.getContentType());
fileU.setPolicyno(policyno);
fileU.setId(UUID.randomUUID().toString());
boolean flag = fileUploadService.insertFile(fileU);
if (flag) {
Result.setStatusText("上传成功");
}else {
Result.setStatusText("上传失败");
}
Result.setData(name);
} catch (FileNotFoundException e) {
logger.error(e.getMessage(), e);
} catch (IOException e) {
logger.error(e.getMessage(), e);
} finally {
try {
// 关闭输入流
if (null != is) {
is.close();
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
} else {
Result.setStatusText("未选择文件");
}
return JSON.toJSONString(Result);
}
private static byte[] toByteArray(InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
copy(input, output);
return output.toByteArray();
}

private static int copy(InputStream input, OutputStream output) throws IOException {
long count = copyLarge(input, output);
if (count > 2147483647L) {
return -1;
}
return (int) count;
}

private static long copyLarge(InputStream input, OutputStream output) throws IOException {
byte[] buffer = new byte[4096];
long count = 0L;
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}

最新文章

  1. Tips for newbie to read source code
  2. java 12 - 5 带有缓冲区的字符流
  3. 【原创】express3.4.8源码解析之路由中间件
  4. Android开发之View动画效果插补器Interpolator
  5. 阿里云部署Docker(7)----将容器连接起来
  6. iPhone开发 - 常用库
  7. MapDB:专为Java设计的高性能的数据库
  8. poj 1664 放苹果_整数拆分
  9. openSUSE13.1安装Nodejs并更新到最新版
  10. JAVA 单步调试快捷键
  11. 在Windows Server 2008 R2下搭建jsp环境(一)
  12. 基于后端云的Android注册登录开发
  13. Filebeat使用内置的mysql模块收集日志存储到ES集群并使用kibana存储
  14. callback源码分析——callback_iter和callback
  15. [转载]css代码优化的12个技巧
  16. PAT乙级 1031. 查验身份证(15)
  17. SCXcodeSwitchExpander自动填充switch语句下枚举类型case
  18. HTML5 Canvas 画纸飞机组件
  19. 自己封装的ajax
  20. 表单数据转换成json格式数据

热门文章

  1. IDEA提交代码到github
  2. Quick BI支持哪些数据源(配置操作篇)
  3. Leetcode 4. Median of Two Sorted Arrays(中位数+二分答案+递归)
  4. IIS发布mvc网站需操作的内容
  5. .NET COM+级别的事务Transaction实现
  6. 对象转json串.
  7. Qt 之 ZIP开源库 QuaZIP
  8. 常见的七种Hadoop和Spark项目案例
  9. Mysql数据库密码忘记的解决办法
  10. 错误 error: The following untracked working tree files would be overwritten by merge:README.md