图片上传工具类
来源:互联网 发布:u盘恢复数据要多少钱 编辑:程序博客网 时间:2024/04/29 16:26
/** * Author: fuchunyu * Date: 2017/6/19 * Description:fileInput上传专用接口 */public class UploadImage extends HttpServlet { private static final long serialVersionUID = 1L; public static String getFilePath() { return "file/" + Convert.toShortYMDWithNoSplit(new Date()) + "/"; } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } private void processRequest(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setCharacterEncoding("UTF-8"); ServletOutputStream os = response.getOutputStream(); // 获得文件保存路径 /* String filePath = getFilePath(); String path = getTempPath(filePath); */ // 临时文件目录 File temp = new File("/usr/local/temp/upload/"); if (!temp.exists()) temp.mkdirs(); // 实例化一个硬盘文件工厂,用来配置上传组件ServletFileUpload DiskFileItemFactory factory = new DiskFileItemFactory(5 * 1024 * 1024, temp); // 用以上工厂实例化上传组件 ServletFileUpload sfu = new ServletFileUpload(factory); // 单个文件最大值 sfu.setSizeMax(5* 1024 * 1024); List fileList = null; try { fileList = sfu.parseRequest(request); } catch (FileUploadException e) { if (e instanceof SizeLimitExceededException) { os.write("文件太大了".getBytes("UTF-8")); os.close(); return; } else { os.write("未知异常".getBytes("UTF-8")); os.close(); return; } } if (fileList == null || fileList.size() == 0) { os.write("请上传文件".getBytes("UTF-8")); os.close(); return; } String fileNames = null; Iterator iter = fileList.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); if (!item.isFormField()) { String fileName = item.getName(); if(fileName.lastIndexOf('.') == -1){ return; } String fileExt = fileName.substring(fileName.lastIndexOf('.')); if (!".png.jpg.jpeg.gif.bmp".contains(fileExt.toLowerCase())) { os.write("文件类型错误".getBytes("UTF-8")); os.close(); return; } fileName = getFileName() + fileExt; try { // 这里将图片上传到oos服务器上面服务器上面 File file = File.createTempFile(fileName, fileExt); file.deleteOnExit(); item.write(file); //file.delete(); //item.write(new File(path, fileName)); } catch (OSSException oe) { System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); System.out.println("Error Message: " + oe.getErrorCode()); System.out.println("Error Code: " + oe.getErrorCode()); System.out.println("Request ID: " + oe.getRequestId()); System.out.println("Host ID: " + oe.getHostId()); } catch (ClientException ce) { System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network."); System.out.println("Error Message: " + ce.getMessage()); } catch (Exception e) { e.printStackTrace(); continue; } finally { /* * Do not forget to shut down the client finally to release all allocated resources. */ client.shutdown(); } // fileName = AppConfig.RESOURCE_APP_ROOT + filePath + fileName; if (fileNames == null) fileNames = fileName; else fileNames += "," + fileName; } } os.write(("success" + "|"+ImagePath + fileNames).getBytes("UTF-8")); os.close(); return; } public static String getTempPath(String filePath) { String projectName = AppConfig.PROJECT_NAME; String ret = AppConfig.getResourceHome(); String contextPath = AppConfig.getContextPath(); if (!contextPath.equals(projectName) && ret.contains(projectName)) { ret = ret.substring(0, ret.indexOf(projectName) - 1); } String path = ret + AppConfig.RESOURCE_APP_ROOT + filePath; // 文件上传目录 File sourceFile = new File(path); if (!sourceFile.exists()) { sourceFile.mkdirs(); } return path; } /** * 获取文件名 * UUID 拼接 8位日期 * * 如需获得原图,可将图片名称截后六位即图片创建日期,然后到file文件夹下根据日期即可找到 * @return */ public static String getFileName(){ String uuid = UUID.randomUUID().toString().replace("-", ""); String time = Convert.toShortYMDWithNoSplit(new Date()); return uuid + time; }}
阅读全文
0 0
- 图片上传工具类
- 图片上传工具类
- 图片文件上传工具类
- Android图片上传工具类
- Android:上传图片工具类
- java上传图片剪切工具类
- Android图片上传的工具类
- 通过ftp上传图片 工具类
- 图片上传到服务器(工具类)
- Android Http请求图片上传工具类
- Android上传图片文件工具类
- 安卓上传图片工具类
- 单张,多张图片上传工具类
- FTP连接图片上传工具类
- 图片上传工具:ImageUtils
- 图片,视频上传工具
- ajaxFileUpload 图片上传工具
- Android 上传图片、上传文件、提交表单 工具类
- 错题
- 调通sina33下的AP6212A0(WIFI+BT)(分色排版)V1.3(android4.4.2)
- 如何解决div里面的h4标题拖拽,并且实现div里面的文本能够选中和不能选中的问题
- dw删除某行 和 撤销删除某行
- vue数据渲染出现闪烁问题及v-cloak没生效
- 图片上传工具类
- js将时间戳格式化
- 暑期C++02之构造函数之调用顺序方法
- 手把手体验 Docker HelloWorld
- iis 发布程序500.19
- CentOS-如何配置centos6.x的ntp时间同步服务器
- clang编译器简介
- 他山界面开发框架OHUI v47.0.2
- ROS-learning-CartoGrapher Installation Steps