| | |
| | | |
| | | import java.io.*; |
| | | import java.net.FileNameMap; |
| | | import java.net.URL; |
| | | import java.net.URLConnection; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.ArrayList; |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 2020-06-29 |
| | | * cjh |
| | | * 图片格式判断 |
| | | * */ |
| | | public static boolean imageFormatJudge(File firs) { |
| | | String imageName = firs.getName(); |
| | | //截取格式 |
| | | String suffix =imageName.substring(imageName.lastIndexOf(".") + 1); |
| | | //格式字母转小写 |
| | | suffix = suffix.toLowerCase(); |
| | | //进行判断 |
| | | if(suffix.equals("png")) { |
| | | return true; |
| | | }else if(suffix.equals("jpg")){ |
| | | return true; |
| | | }else if(suffix.equals("jpeg")){ |
| | | return true; |
| | | }else { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | /**视频上传的方法 |
| | | * 保存到服务器里面的 |
| | | * @param platformIconFile 视频文件 |
| | |
| | | * @return 图片路径 |
| | | * 2020-06-29 ChenJiaHe |
| | | */ |
| | | public static String handleFileUpload(File platformIconFile,String unifiedFolder,String saveFolder |
| | | ,boolean autoDateFolder) { |
| | | String fileName = ""; |
| | | try { |
| | | if(platformIconFile == null) { |
| | | throw new TipsException("请上传图片!"); |
| | | } |
| | | if(!imageFormatJudge(platformIconFile)) { |
| | | throw new TipsException("请上传png、jpg和jpeg格式的图片!"); |
| | | } |
| | | |
| | | //设置图片大小 |
| | | // String.format("%.1f",platformIconFile.getSize()/1024.0); |
| | | if(autoDateFolder){ |
| | | if(saveFolder.endsWith("/")){ |
| | | saveFolder = saveFolder+dateFormat(new Date(),"yyyyMM")+"/"; |
| | | }else{ |
| | | saveFolder = saveFolder+"/"+dateFormat(new Date(),"yyyyMM")+"/"; |
| | | } |
| | | } |
| | | fileName = dateFormat(new Date(),"yyyyMMddHHmmssSSS"); |
| | | if(unifiedFolder.endsWith("/")){ |
| | | if(saveFolder.startsWith("/")){ |
| | | saveFolder = saveFolder.replaceFirst("/",""); |
| | | unifiedFolder = unifiedFolder + saveFolder; |
| | | }else{ |
| | | unifiedFolder = unifiedFolder+saveFolder; |
| | | } |
| | | }else{ |
| | | if(saveFolder.startsWith("/")){ |
| | | unifiedFolder = unifiedFolder + saveFolder; |
| | | }else{ |
| | | unifiedFolder = unifiedFolder+"/"+saveFolder; |
| | | } |
| | | } |
| | | fileName = saveFolder+fileUp(platformIconFile,unifiedFolder,fileName); |
| | | } catch (RuntimeException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return fileName; |
| | | } |
| | | |
| | | /**图片上传的方法 |
| | | * 保存到服务器里面的 |
| | | * @param platformIconFile 图片文件 |
| | | * @param unifiedFolder NG指向的前端文件夹(统一文件夹),如:user/local/images/ |
| | | * @param saveFolder 保存到的文件夹,如:/bananer/ |
| | | * @param autoDateFolder 是否生成日期文件夹 |
| | | * @return 图片路径 |
| | | * 2020-06-29 ChenJiaHe |
| | | */ |
| | | public static String handleFileUpload(MultipartFile platformIconFile,String unifiedFolder,String saveFolder |
| | | ,boolean autoDateFolder) { |
| | | String fileName = ""; |
| | |
| | | } |
| | | |
| | | //设置图片大小 |
| | | // String.format("%.1f",platformIconFile.getSize()/1024.0); |
| | | // String.format("%.1f",platformIconFile.getSize()/1024.0); |
| | | if(autoDateFolder){ |
| | | if(saveFolder.endsWith("/")){ |
| | | saveFolder = saveFolder+dateFormat(new Date(),"yyyyMM")+"/"; |
| | |
| | | } |
| | | |
| | | /** |
| | | * 音频上传 |
| | | * @param platformIconFile |
| | | * @param unifiedFolder |
| | | * @param saveFolder |
| | | * @param autoDateFolder |
| | | * @return |
| | | */ |
| | | public static String handleAudioUpload(MultipartFile platformIconFile,String unifiedFolder,String saveFolder |
| | | ,boolean autoDateFolder) { |
| | | String fileName = ""; |
| | | try { |
| | | if(platformIconFile == null) { |
| | | throw new TipsException("请上传音频!"); |
| | | } |
| | | |
| | | if(autoDateFolder){ |
| | | if(saveFolder.endsWith("/")){ |
| | | saveFolder = saveFolder+dateFormat(new Date(),"yyyyMM")+"/"; |
| | | }else{ |
| | | saveFolder = saveFolder+"/"+dateFormat(new Date(),"yyyyMM")+"/"; |
| | | } |
| | | } |
| | | |
| | | fileName = dateFormat(new Date(),"yyyyMMddHHmmssSSS"); |
| | | if(unifiedFolder.endsWith("/")){ |
| | | if(saveFolder.startsWith("/")){ |
| | | saveFolder = saveFolder.replaceFirst("/",""); |
| | | unifiedFolder = unifiedFolder + saveFolder; |
| | | }else{ |
| | | unifiedFolder = unifiedFolder+saveFolder; |
| | | } |
| | | }else{ |
| | | if(saveFolder.startsWith("/")){ |
| | | unifiedFolder = unifiedFolder + saveFolder; |
| | | }else{ |
| | | unifiedFolder = unifiedFolder+"/"+saveFolder; |
| | | } |
| | | } |
| | | fileName = saveFolder+fileUp(platformIconFile,unifiedFolder,fileName); |
| | | } catch (RuntimeException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return fileName; |
| | | } |
| | | |
| | | /** |
| | | * 文件上传 |
| | | * @param platformIconFile |
| | | * @param unifiedFolder |
| | | * @param saveFolder |
| | | * @param autoDateFolder |
| | | * @return |
| | | */ |
| | | public static String handleOtherFileUpload(MultipartFile platformIconFile,String unifiedFolder,String saveFolder |
| | | ,boolean autoDateFolder) { |
| | | String fileName = ""; |
| | | try { |
| | | if(platformIconFile == null) { |
| | | throw new TipsException("请上传文件!"); |
| | | } |
| | | |
| | | if(autoDateFolder){ |
| | | if(saveFolder.endsWith("/")){ |
| | | saveFolder = saveFolder+dateFormat(new Date(),"yyyyMM")+"/"; |
| | | }else{ |
| | | saveFolder = saveFolder+"/"+dateFormat(new Date(),"yyyyMM")+"/"; |
| | | } |
| | | } |
| | | |
| | | fileName = dateFormat(new Date(),"yyyyMMddHHmmssSSS"); |
| | | if(unifiedFolder.endsWith("/")){ |
| | | if(saveFolder.startsWith("/")){ |
| | | saveFolder = saveFolder.replaceFirst("/",""); |
| | | unifiedFolder = unifiedFolder + saveFolder; |
| | | }else{ |
| | | unifiedFolder = unifiedFolder+saveFolder; |
| | | } |
| | | }else{ |
| | | if(saveFolder.startsWith("/")){ |
| | | unifiedFolder = unifiedFolder + saveFolder; |
| | | }else{ |
| | | unifiedFolder = unifiedFolder+"/"+saveFolder; |
| | | } |
| | | } |
| | | fileName = saveFolder+fileUp(platformIconFile,unifiedFolder,fileName); |
| | | } catch (RuntimeException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return fileName; |
| | | } |
| | | |
| | | /** |
| | | * 2020-06-29 ChenJiaHe |
| | | * @param file //文件对象 |
| | | * @param filePath //上传路径 |
| | | * @param fileName //文件名 |
| | | * @return 文件名 |
| | | */ |
| | | * @param file //文件对象 |
| | | * @param filePath //上传路径 |
| | | * @param fileName //文件名 |
| | | * @return 文件名 |
| | | */ |
| | | public static String fileUp(MultipartFile file, String filePath, String fileName){ |
| | | String extName = ""; // 扩展名格式: |
| | | try { |
| | |
| | | return fileName+extName; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 写文件到当前目录的upload目录中 |
| | | * |
| | | * @param in |
| | | * @param fileName |
| | | * @throws IOException |
| | | * 2020-06-29 ChenJiaHe |
| | | * @param file //文件对象 |
| | | * @param filePath //上传路径 |
| | | * @param fileName //文件名 |
| | | * @return 文件名 |
| | | */ |
| | | public static String fileUp(File file, String filePath, String fileName){ |
| | | String extName = ""; // 扩展名格式: |
| | | try { |
| | | if (file.getName().lastIndexOf(".") >= 0){ |
| | | extName = file.getName().substring(file.getName().lastIndexOf(".")); |
| | | } |
| | | copyFile(file, filePath, fileName+extName).replaceAll("-", ""); |
| | | } catch (IOException e) { |
| | | System.out.println(e); |
| | | } |
| | | return fileName+extName; |
| | | } |
| | | |
| | | /** |
| | | * 写文件到当前目录的upload目录中 |
| | | * |
| | | * @param in |
| | | * @param fileName |
| | | * @throws IOException |
| | | */ |
| | | private static String copyFile(InputStream in, String dir, String realName)throws IOException { |
| | | File file = new File(dir, realName); |
| | | file.setWritable(true); |
| | |
| | | file.createNewFile(); |
| | | } |
| | | org.apache.commons.io.FileUtils.copyInputStreamToFile(in, file); |
| | | return realName; |
| | | } |
| | | |
| | | /** |
| | | * 写文件到当前目录的upload目录中 |
| | | * |
| | | * @param in |
| | | * @param fileName |
| | | * @throws IOException |
| | | */ |
| | | private static String copyFile(File fileIn, String dir, String realName)throws IOException { |
| | | File file = new File(dir, realName); |
| | | file.setWritable(true); |
| | | if (!file.exists()) { |
| | | if (!file.getParentFile().exists()) { |
| | | file.getParentFile().mkdirs(); |
| | | } |
| | | file.createNewFile(); |
| | | } |
| | | org.apache.commons.io.FileUtils.copyFile(fileIn,file); |
| | | return realName; |
| | | } |
| | | |
| | |
| | | return type; |
| | | } |
| | | |
| | | public static File httpUrlFile(String netUrl) throws IOException { |
| | | File file = File.createTempFile("temp123", ".xls"); |
| | | InputStream inStream = null; |
| | | FileOutputStream os = null; |
| | | |
| | | try { |
| | | file = File.createTempFile("net_url", ".jpg"); |
| | | URL urlfile = new URL(netUrl); |
| | | inStream = urlfile.openStream(); |
| | | os = new FileOutputStream(file); |
| | | |
| | | byte[] buffer = new byte[8192]; |
| | | |
| | | int bytesRead; |
| | | while((bytesRead = inStream.read(buffer, 0, 8192)) != -1) { |
| | | os.write(buffer, 0, bytesRead); |
| | | } |
| | | } catch (Exception var15) { |
| | | var15.printStackTrace(); |
| | | } finally { |
| | | try { |
| | | if (null != os) { |
| | | os.close(); |
| | | } |
| | | |
| | | if (null != inStream) { |
| | | inStream.close(); |
| | | } |
| | | } catch (Exception var14) { |
| | | var14.printStackTrace(); |
| | | } |
| | | |
| | | } |
| | | |
| | | return file; |
| | | } |
| | | |
| | | } |