Iterator<FileItem>的使用,实现文件(图片)上传,限制图片格式,设置随机文件名
来源:互联网 发布:linux redis 远程访问 编辑:程序博客网 时间:2024/06/01 10:53
public StringMap uploadPicture(HttpServletRequest request, HttpServletResponse response)throws Exception{ StringMap map=new StringMap(); DiskFileItemFactory fac = new DiskFileItemFactory(); // 创建servlet文件上传组件 ServletFileUpload upload = new ServletFileUpload(fac); ListfileInfoList = null; try { fileInfoList = upload.parseRequest(request); } catch (FileUploadException ex) { ex.printStackTrace(); } Iterator paramItem = fileInfoList.iterator(); Map paraMap = new HashMap (); while (paramItem.hasNext()) { //获取域中传入的普通参数 FileItem item = paramItem.next(); if (item.isFormField()){ String filedName = item.getFieldName(); String value = ""; try { value = item.getString("UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } paraMap.put(filedName, value); } } InetAddress addr = InetAddress.getLocalHost(); String localIp=addr.getHostAddress().toString();//获得本机IP String pictureUrl=""; Iterator fileItem = fileInfoList.iterator(); String[] allowedExt = new String[] { "jpg", "jpeg", "gif", "png"}; while (fileItem.hasNext()){ //获取域中传入的文件参数 FileItem item = fileItem.next(); if (!item.isFormField()) { String filename = item.getName(); //得到文件名 String fileExt = filename.substring(filename.lastIndexOf(".") + 1); //得到文件格式 boolean isPiceure=false; //上传的文件是否图片 int allowedExtCount = allowedExt.length; for (int i=0; i< allowedExtCount; i++) { if (allowedExt[i].toLowerCase().equals(fileExt.toLowerCase())) isPiceure=true; } if (isPiceure) { String[] s = filename.split("\\.", 2); String fileName = System.currentTimeMillis()+ "." + s[1]; //s[1]为文件后缀名,这里文件名设置为随机值 try { String tomcatPath = "../webapps/ROOT"; //找到tomcat目录 File uploadDirPath = new File(tomcatPath + "/upload"); if (!uploadDirPath.exists()) { uploadDirPath.mkdirs(); File uploadDirPath_tmp = new File(tomcatPath + "/upload"); if (!uploadDirPath_tmp.exists()) { uploadDirPath_tmp.mkdir(); } } String serverpath = "http://" + localIp + ":8080"; //设置要上传的服务器地址和端口号; pictureUrl = serverpath + "/upload/" + fileName; //设置全路径 File copyfile = new File(tomcatPath + "/upload/" + fileName); if (!copyfile.exists()) { item.write(copyfile); //FileCopyUtils来自org.springframework.util.FileCopyUtils } } catch (Exception e) { e.printStackTrace(); } map.set("msg","图片上传成功!"); map.set("pictureUrl",pictureUrl); } else{ map.set("msg","图片上传失败或图片格式不正确!"); return map; } }} return map; }
0 0
- Iterator<FileItem>的使用,实现文件(图片)上传,限制图片格式,设置随机文件名
- FileItem 类(文件上传)
- 如何用FileItem来实现文件上传
- simditor 限制图片格式 和 上传图片过慢
- JS实现点击某张图片弹出上传图片窗口以及限制图片格式和大小
- 设置文件上传限制(解除限制)
- 上传图片过滤掉不是图片格式的文件
- 使用js实现上传图片的大小限制
- Java 文件上传,使用FileItem解析时,出现乱码的解决办法
- 用HTML的File控件上传图片,用JS判断和限制图片格式、类型、尺寸
- 图片上传(判读图片格式,重命名文件)
- JavaScript图片上传前预览和限制图片格式
- 设置IIS7文件上传的限制
- 使用Struts2上传时,一定要设置 文件上传大小限制。
- 使用FileItem获取文件名时注意事项
- 使用Struts实现文件上传,格式限制,大小限制
- 上传图片的限制
- javascript实现限制上传文件的大小
- 安卓开发—使用有道翻译
- 一入编程深似海
- Android ViewDragHelper完全解析 自定义ViewGroup神器
- 记录安装Urho3D的过程
- java数据结构(二)——Set(HashSet,TreeSet),泛型,Map(HashMap,TreeMap)
- Iterator<FileItem>的使用,实现文件(图片)上传,限制图片格式,设置随机文件名
- 【腾讯Bugly干货分享】经典随机Crash之二:Android消息机制
- Apache安装以及配置
- NGINX
- POJ 3190 Stall Reservations
- waitKey (x)的两个作用
- codeforces710c Magic Odd Square
- 题目1107:搬水果
- LeetCode-515. Find Largest Value in Each Tree Row (JAVA)(二叉树每行的最大值)