上传文件


@RequestMapping(value = "/_doSavePIOrder", method = RequestMethod.POST)
public ModelAndView doSavePIOrder(HttpSession session, HttpServletRequest request, HttpServletResponse response) {
ModelAndView mv = new ModelAndView();
ResultObj result = new ResultObj();
result.setCmd("doSavePIOrder");
result.setAjax(StringUtil.getParam(request, "ajax", false, false));
String fileType = request.getParameter("fileType");
String maxSize = "10";
String tempPath = request.getSession().getServletContext().getRealPath("/");
ShopUserObj user = ShopUtil.getSession(session);
try {
printHeaders(request);
if (hasUploadFile(request) && user != null) {
DiskFileItemFactory factory = new DiskFileItemFactory();
// 最大缓存
factory.setSizeThreshold(5 * 1024);
// 设置临时文件目录
factory.setRepository(new File(tempPath));
ServletFileUpload upload = new ServletFileUpload(factory);
if (maxSize != null && !"".equals(maxSize.trim())) {
// 文件最大上限
upload.setSizeMax(Integer.valueOf(maxSize) * 1024 * 1024);
}
// 获取所有文件列表
List<FileItem> items = upload.parseRequest(request);
if (items != null) {
List<UploadObj> uploads = new ArrayList<UploadObj>();
for (int i = 0; i < items.size(); i++) {
FileItem item = items.get(i);
if (!item.isFormField()) {
// 文件名
String fileName = item.getName();
// 检查文件后缀格式
if (!StringUtil.isEmpty(fileName) && !StringUtil.isEmpty(item.getContentType())) {

UploadObj obj = new UploadObj();
obj.setFileNameOld(item.getName());
obj.setFileSize(item.getSize());
obj.setFileExt(getFileExt(obj.getFileNameOld()));
obj.setCreateAt(new Date());
obj.setCreateBy(user.getId());
obj.setUpdateAt(obj.getCreateAt());
obj.setUpdateBy(obj.getCreateBy());
obj.setUploadPath(DateUtil.getDateTime(obj.getCreateAt(), "yyyy/MM/dd/"));
obj.setFileName(DateUtil.getDateTime(obj.getCreateAt(), "HHmmss") + StringUtil.getRandomString(5));
obj.setFileType(getFileType(obj.getFileExt()));
getImageSize(obj, item);
UploadSQL.getInstance().doCreate(obj);
StringBuffer path = new StringBuffer();
path.append(request.getSession().getServletContext().getRealPath("/f")).append("/");
path.append(obj.getUploadPath()).append(obj.getFileName()).append(".").append(obj.getFileExt());
// 写入文件
File file = new File(path.toString());
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
item.write(file);
uploads.add(obj);
}
}
}
quotationService.updatePIOrder(session, items, result, user, uploads);
}

}
} catch (Exception e) {
e.printStackTrace();
}
mv.addObject("result", result);
mv.setViewName("shopwork");
return mv;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值