博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
文件的上传(可以上传照片,word文档,等单个文件)
阅读量:4330 次
发布时间:2019-06-06

本文共 4224 字,大约阅读时间需要 14 分钟。

jsp:jsp页面:
@RequestMapping("core/upload!uploadFile.action")    public void uploadFile(@RequestParam(value = "uploadify", required = false) MultipartFile uploadify,HttpServletRequest request,HttpServletResponse response,ModelMap modelMap) throws Exception {        @SuppressWarnings("deprecation")        String savePath = request.getRealPath("/")+ "/" + "upload/";        String saveUrl = "upload/";        HashMap
extMap = new HashMap
(); extMap.put("file", "pdf,rar,zip,7z,gif,jpg,jpeg,png,bmp,doc,docx,xlsx,xls"); //extMap.put("image", "gif,jpg,jpeg,png,bmp"); extMap.put("csv", "csv"); int uploadMaxSize =Integer.valueOf(optionService.getByOptionName(CoreValue.OPTION_UPLOAD_MAX_SIZE).getOptionValue()); long maxSize = Long.valueOf(String.valueOf(uploadMaxSize)).longValue(); String maxSizeKb = StringUtil.formatNumber(new Double(maxSize / 1024L),"0"); response.setContentType("text/html; charset=UTF-8"); String jsonString = ""; if (!ServletFileUpload.isMultipartContent(getRequest())) { jsonString = getError("请选择文件。"); response.getWriter().println(jsonString); return; } File uploadDir = new File(savePath); if (!uploadDir.isDirectory()) { jsonString = getError(savePath + "上传目录不存在。"); response.getWriter().println(jsonString); return; } if (!uploadDir.canWrite()) { jsonString = getError("上传目录没有写权限。"); response.getWriter().println(jsonString); return; } String dirName = getRequest().getParameter("dir"); if (dirName == null) { dirName = "file"; } if (!extMap.containsKey(dirName)) { jsonString = getError("目录名不正确。"); response.getWriter().println(jsonString); return; } savePath = savePath + dirName + "/"; saveUrl = saveUrl + dirName + "/"; File saveDirFile = new File(savePath); if (!saveDirFile.exists()) { saveDirFile.mkdirs(); } SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); String ymd = sdf.format(new Date()); savePath = savePath + ymd + "/"; saveUrl = saveUrl + ymd + "/"; File dirFile = new File(savePath); if (!dirFile.exists()) { dirFile.mkdirs(); } String fileName = uploadify.getOriginalFilename(); long fileSize = uploadify.getSize(); if (fileSize > maxSize) { jsonString = getError("上传文件大小超过限制。最大为" + maxSizeKb + "kb"); response.getWriter().println(jsonString); return; } String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase(); if (!Arrays.asList(((String) extMap.get(dirName)).split(",")).contains(fileExt)) { jsonString = getError("不允许的上传文件类型。"); response.getWriter().println(jsonString); return; } SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss"); String newFileName = df.format(new Date()) + "_"+ new Random().nextInt(1000) + "." + fileExt; File uploadedFile = null; try { uploadedFile = new File(savePath, newFileName); if(uploadedFile.exists()){ } if(!uploadedFile.exists()){ uploadedFile.mkdirs(); } uploadify.transferTo(uploadedFile); } catch (Exception e) { jsonString = getError("上传文件失败。"); response.getWriter().println(jsonString); return; } JSONObject obj = new JSONObject(); obj.put("error", Integer.valueOf(0)); obj.put("url", saveUrl + newFileName); obj.put("newFileName", newFileName); obj.put("fileName", fileName); jsonString = obj.toJSONString(); response.getWriter().println(jsonString); } private String getError(String message) { JSONObject obj = new JSONObject(); obj.put("error", Integer.valueOf(1)); obj.put("message", message); return obj.toJSONString(); }

 

转载于:https://www.cnblogs.com/lxnlxn/p/10007332.html

你可能感兴趣的文章
ScrollView
查看>>
Oracle入门知识
查看>>
zookeeper
查看>>
c# ADO.NET
查看>>
python安装virtualenv
查看>>
题解 P1073 【最优贸易】
查看>>
053-006
查看>>
字符串分割
查看>>
20175318 2018-2019-2 《Java程序设计》第九周学习总结
查看>>
根据Model有值的自动生成添加的Sql语句
查看>>
hdu 1850 Being a Good Boy in Spring Festival (博弈)
查看>>
Java的堆栈和堆
查看>>
GIL 线程/进程池 同步异步
查看>>
plsql 只有三个文本框,无法登陆
查看>>
百度文库:网站镜像站点解决方案
查看>>
Spring集成Mybatis(Dao方式开发)
查看>>
python调用shell命令
查看>>
jQuery实现点击单选按钮切换选中状态效果
查看>>
COGS314. [NOI2004] 郁闷的出纳员
查看>>
源码安装mysql,及主从同步
查看>>