单文件上传与微信多媒体文件转码
项目中用到的文件上传和微信录音持久化到本地
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; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何调试 malloc 的底层源码
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 因为Apifox不支持离线,我果断选择了Apipost!
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端