package com.java.activiti.util; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.*; import org.apache.commons.lang.StringUtils; import org.springframework.web.multipart.MultipartFile; import sun.misc.BASE64Decoder; import javax.servlet.http.HttpServletRequest; /** * 图片上传 * linux下可自动赋权限,可判断windows or linux 按日期分夹存储 * @author szd * */ public class Upload { private static Upload upload = null; public static Upload getInstance(){ if (upload==null) { upload=new Upload(); } return upload; } /** * 图片上传 * @param * @param returnUrl * @param path * @param strFile * @param osPlatform * @return * @throws IOException */ public static String uploadImage(String image,String lastName, String returnUrl, String path, String strFile, EPlatform osPlatform, String strPermission) throws IOException { List<String> imgList=new ArrayList<String>(); String imagebyte = image; String fileF = lastName; String msg = "";//返回存储路径 if((fileF != null && fileF != "") && (imagebyte != null && imagebyte != "")) { File targetFile = null; String fileName = new Date().getTime() + "_" + new Random().nextInt(1000) + fileF;//新的文件名 //先判断文件是否存在 SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");//设置日期格式 String fileAdd = df.format(new Date()); File file1 = new File(path + "/" + fileAdd); //如果文件夹不存在则创建 if (!file1.exists() && !file1.isDirectory()) { file1.mkdir(); } targetFile = new File(file1, fileName); // 解码,然后将字节转换为文件 byte[] bytes = new BASE64Decoder().decodeBuffer(imagebyte); ByteArrayInputStream ins = new ByteArrayInputStream(bytes); byte[] buffer = new byte[1024]; FileOutputStream out = new FileOutputStream(targetFile); int bytesum = 0; int byteread = 0; while ((byteread = ins.read(buffer)) != -1) { bytesum += byteread; out.write(buffer, 0, byteread); //文件写操作 } out.close(); //file[i].transferTo(targetFile); msg = returnUrl + fileAdd + "/" + fileName; imgList.add(msg); } //判断当前操作系统是否是windows /*if(!"Windows".equals(osPlatform.toString())){ ProcessUitl.getInstance().chmod("-R 755", strPermission); }*/ return msg; } public static List<String> imgUpload(HttpServletRequest request,MultipartFile[] file,String returnUrl) throws IOException { //String strFile="pwmgr"+File.separator+"imgs"; //这个就是绝对路径 String strFile="partyworkmgr"; //这个就是绝对路径 String strPermission="/partyworkmgr/"; String strPath =""; //获取当前操作系统 EPlatform osPlatform= OSinfo.getOSname(); if(!"Windows".equals(osPlatform.toString())){ //linux 下存储路径 File fDir=new File(File.separator); //File.separator表示根目录,比如现在就表示在D盘下。 File f=new File(fDir,strFile); strPath = f.toString(); //文件存储位置 }else{ //windows 操作系统下文件存储位置 strPath = request.getSession().getServletContext().getRealPath("upload/imgs"); //文件存储位置 File filePath = new File(strPath); if(!filePath.exists()) { filePath.mkdirs(); } } //List<String> imgList= Upload.getInstance().uploadImage(file,returnUrl,strPath,strFile,osPlatform,strPermission); return null; } // public static Map<String,Object> imgUpload (Map<String,Object> map,String returnUrl,HttpServletRequest request) throws IOException { String strFile="partyworkmgr"; //这个就是绝对路径 String strPermission="/partyworkmgr/"; String strPath =""; //获取当前操作系统 EPlatform osPlatform= OSinfo.getOSname(); if(!"Windows".equals(osPlatform.toString())){ //linux 下存储路径 File fDir=new File(File.separator); //File.separator表示根目录,比如现在就表示在D盘下。 File f=new File(fDir,strFile); strPath = f.toString(); //文件存储位置 }else{ //windows 操作系统下文件存储位置 strPath = request.getSession().getServletContext().getRealPath("upload/imgs"); //文件存储位置 File filePath = new File(strPath); if(!filePath.exists()) { filePath.mkdirs(); } } List<Map<String,String>> newList = new ArrayList<>(); String image1 = (String) map.get("image1"); String image2 = (String) map.get("image2"); String image3 = (String) map.get("image3"); String image4 = (String) map.get("image4"); String image5 = (String) map.get("image5"); if (StringUtils.isNotBlank(image1)){ Map<String,String> map1 = new HashMap<>(); map1.put(image1,map.get("lastName1").toString()); newList.add(map1); } if (StringUtils.isNotBlank(image2)){ Map<String,String> map1 = new HashMap<>(); map1.put(image2,map.get("lastName2").toString()); newList.add(map1); } if (StringUtils.isNotBlank(image3)){ Map<String,String> map1 = new HashMap<>(); map1.put(image3,map.get("lastName3").toString()); newList.add(map1); } if (StringUtils.isNotBlank(image4)){ Map<String,String> map1 = new HashMap<>(); map1.put(image4,map.get("lastName4").toString()); newList.add(map1); } if (StringUtils.isNotBlank(image5)){ Map<String,String> map1 = new HashMap<>(); map1.put(image5,map.get("lastName5").toString()); newList.add(map1); } List<String> imgList = new ArrayList<>(); for (Map<String,String> im : newList) { for (Map.Entry<String, String> entry : im.entrySet()) { String s = Upload.uploadImage(entry.getKey(), entry.getValue(), returnUrl, strPath, strFile, osPlatform, strPermission); imgList.add(s); } } /*for (Map.Entry<String, String> entry : map1.entrySet()) { String s = imgList.add(s); }*/ //List<String> imgList = Upload.imgUpload(request, file, returnUrl); switch (imgList.size()) { case 1: map.put("image1",imgList.get(0)); break; case 2: map.put("image1",imgList.get(0)); map.put("image2",imgList.get(1)); break; case 3: map.put("image1",imgList.get(0)); map.put("image2",imgList.get(1)); map.put("image3",imgList.get(2)); break; case 4: map.put("image1",imgList.get(0)); map.put("image2",imgList.get(1)); map.put("image3",imgList.get(2)); map.put("image4",imgList.get(3)); break; case 5: map.put("image1",imgList.get(0)); map.put("image2",imgList.get(1)); map.put("image3",imgList.get(2)); map.put("image4",imgList.get(3)); map.put("image5",imgList.get(4)); default: break; } return map; } }