//文件上传 public FileInfo[] addPhotoFileNew(String[] names, String mkDirectory, Long sbmh, Long sysyear, String[] types) { String message = ""; mkDirectory = mkDirectory.toUpperCase(); FileInfo[] FileInBean = new FileInfo[names.length]; List<SystemParameter> list = commonService.getSysByCodeclass("SYS_" + mkDirectory + "_FILEUPLODFORMAT", null, sysyear); ArrayList<String> arr = new ArrayList<String>(); this.setIsok(true); for (int i = 0; i < names.length; i++) { String filename = get(names[i] + "FileName"); // 文件名称 String filetype = get(names[i] + "ContentType"); // 文件格式 String lastname = (filename == null ? null : filename.substring(filename.lastIndexOf("."), filename.length()));// 文件名后缀 // 判断文件类型是否复合 if (types != null && filename != null) { List<String> typelist = Arrays.asList(types); if (!typelist.contains(lastname)) { // 类型提示不符合 String typestr = Arrays.toString(types); this.isok = false; this.setMessage("上传文件格式错误,上传“" + typestr.substring(1, typestr.length() - 1) + "”格式的文件!"); return null; } } File[] file = (File[]) ActionContext.getContext().getParameters().get(names[i]); // 文件 // this.setFile(fil); if (file != null && file.length > 0) { if (!(filename == null || "".equals(filename))) { if (isFomartTrue(list, filetype)) { Date dat = new Date(); Random random = new Random(99); int k = random.nextInt(100); String wjName = sbmh + "" + dat.getSeconds() + "" + k; try { String file_Path = this.getUploadDirectory() + "/" + mkDirectory + "/" + wjName + filename.substring(filename.indexOf("."), filename.length()); String file_Path_New = this.getUploadDirectory() + "/" + mkDirectory + "/" + wjName+"_new" + filename.substring(filename.indexOf("."), filename.length()); FileUtils.copyFile(file[0], new File(file_Path)); compressPic(file_Path,file_Path_New); // File imgfile = new File(file_Path_New); // 读入文件 //BufferedImage src = javax.imageio.ImageIO.read(imgfile); // 构造Image对象 arr.add(file_Path); FileInBean[i] = new FileInfo(); FileInBean[i].setFilename(filename); FileInBean[i].setFileformat(filetype); FileInBean[i].setFilepath(file_Path_New); FileInBean[i].setFiletitle(filename.substring(0, filename.indexOf("."))); file[0].delete(); } catch (Exception e) { e.printStackTrace(); this.setMessage(message + "第 " + (i + 1) + " 个文件上传失败!"); this.setIsok(false); break; } } else { this.setMessage(message + "第 " + (i + 1) + " 个文件上传失败,文件格式不正确"); this.setIsok(false); break; } } else { this.setMessage(message + "第 " + (i + 1) + " 个文件上传失败,该文件不存在 "); this.setIsok(false); break; } } else { this.setMessage(message + "第 " + (i + 1) + " 个文件上传失败,该文件不存在 "); this.setIsok(false); break; } } if (!this.getIsok()) { for (String Fil : arr) { File f = new File(Fil); f.delete(); } } return FileInBean; }
//图片压缩后上传方法
public static boolean compressPic(String srcFilePath, String descFilePath) throws IOException { File file = null; BufferedImage src = null; FileOutputStream out = null; ImageWriter imgWrier; ImageWriteParam imgWriteParams; // 指定写图片的方式为 jpg imgWrier = ImageIO.getImageWritersByFormatName("jpg").next(); imgWriteParams = new javax.imageio.plugins.jpeg.JPEGImageWriteParam( null); // 要使用压缩,必须指定压缩方式为MODE_EXPLICIT imgWriteParams.setCompressionMode(imgWriteParams.MODE_EXPLICIT); // 这里指定压缩的程度,参数qality是取值0~1范围内, imgWriteParams.setCompressionQuality((float)0.4); imgWriteParams.setProgressiveMode(imgWriteParams.MODE_DISABLED); ColorModel colorModel = ImageIO.read(new File(srcFilePath)).getColorModel();// ColorModel.getRGBdefault(); // 指定压缩时使用的色彩模式 imgWriteParams.setDestinationType(new javax.imageio.ImageTypeSpecifier( colorModel, colorModel.createCompatibleSampleModel(16, 16))); // imgWriteParams.setDestinationType(new javax.imageio.ImageTypeSpecifier( // colorModel, colorModel.createCompatibleSampleModel(16, 16))); try { if (isBlank(srcFilePath)) { return false; } else { file = new File(srcFilePath); System.out.println(file.length()); src = ImageIO.read(file); out = new FileOutputStream(descFilePath); imgWrier.reset(); // 必须先指定 out值,才能调用write方法, ImageOutputStream可以通过任何 // OutputStream构造 imgWrier.setOutput(ImageIO.createImageOutputStream(out)); // 调用write方法,就可以向输入流写图片 imgWrier.write(null, new IIOImage(src, null, null), imgWriteParams); out.flush(); out.close(); } } catch (Exception e) { e.printStackTrace(); return false; } return true; } public static boolean isBlank(String string) { if (string == null || string.length() == 0 || string.trim().equals("")) { return true; } return false; }