单文件上传与微信多媒体文件转码

项目中用到的文件上传和微信录音持久化到本地
public
class FileOperateUtil { private static final String fileServerUrl = PropertiesUtil.get("File.fileServerUrl"); private static final String fileNetServerUrl = PropertiesUtil.get("File.fileNetServerUrl"); /** * arm 转码 mp3 * @param uploadFile * @throws Exception */ private static boolean armToMp3(File uploadFile) throws Exception{ boolean trans = false; String sourePath = uploadFile.getCanonicalPath(); String outPath = sourePath.substring(0, sourePath.lastIndexOf(".")).concat(".mp3"); List<String> cmd = new ArrayList<String>(); String os = System.getProperties().getProperty("os.name"); if(os.indexOf("Mac") >= 0){ cmd.add("/usr/local/bin/ffmpeg"); //ffmpeg 可执行地址 }else if(os.indexOf("Linux") >= 0){ cmd.add("/usr/bin/ffmpeg"); //ffmpeg 可执行地址 }else{ cmd.add("C:/Program Files/ffmpeg.exe"); //ffmpeg 可执行地址 } cmd.add("-i"); cmd.add(sourePath); cmd.add("-y"); cmd.add(outPath); StringBuffer getter = new StringBuffer(); for(int i=0;i<3;i++){ boolean mp3 = ExecutiveShell.exec(cmd,getter); if(mp3){ trans = true; break; } } if(!trans){ trans = false; uploadFile.deleteOnExit(); throw new Exception("语音转码失败!"); }else{ return trans; } } /** * @描述 单文件上传 * @日期 2016年11月22日 下午11:12:57 * @version 1.0 * @param in * @param fileName * @return */ public static String singleFileUpload(InputStream in,String fileName){ String returnUrl = null; String upLoadUrl = null; OutputStream out = null; File file = null; byte[] buf = new byte[1024 * 10]; int length = 0; // 每个月的上传文件都分开 String filePath = new SimpleDateFormat("yyyy-MM").format(new Date()); try{ //获取文件类型 String type = fileName.substring(fileName.indexOf('.')+1); //根据文件类型分开 filePath = "/"+filePath + "/"+type+"/"; upLoadUrl = fileServerUrl + filePath; //文件夹判断补全 file = new File(upLoadUrl); file.mkdirs(); file = new File(upLoadUrl+fileName); out = new FileOutputStream(file); while ((length = in.read(buf)) != -1) { out.write(buf, 0, length); out.flush(); } returnUrl =fileNetServerUrl + filePath+fileName; out.close(); in.close(); }catch(Exception e){ returnUrl = null; }finally{ try{ buf = null; if (out != null){ out.close(); } if (in != null){ in.close(); } if(armToMp3(file)){ returnUrl = returnUrl.substring(0,returnUrl.lastIndexOf(".")).concat(".mp3"); } }catch(Exception e){ } } return returnUrl; } }

 

posted @ 2016-12-27 14:30  微飞翔  阅读(335)  评论(0编辑  收藏  举报