注:本人开始运行下面报 java.io.FileNotFoundException ,纠结很久后清理tomcat后运行成功
//获取wav文件地址 String vRecordUrl=(request.getParameter("vRecordUrl")==null?"":request.getParameter("vRecordUrl")); LogUtil.writeLog("getRecord callback vRecordUrl"+vRecordUrl); //通话sessionId String vSessionId=(request.getParameter("vSessionId")==null?"":request.getParameter("vSessionId")); LogUtil.writeLog("getRecord callback vSessionId"+vSessionId); //获取文件名称 File file=new File(vRecordUrl); String fileName=file.getName(); //获取项目路径 //String path=request.getServletContext().getRealPath("/")+"WEB-INF/res/records/"; String path = request.getSession().getServletContext().getRealPath("/WEB-INF/res/records/"); URL url = new URL(vRecordUrl); // 打开连接 HttpURLConnection con = (HttpURLConnection) url.openConnection(); //设置请求超时为5s con.setConnectTimeout(5*1000); // 输入流 InputStream is = con.getInputStream(); // 1K的数据缓冲 byte[] bs = new byte[1024]; // 读取到的数据长度 int len; // 输出的文件流 OutputStream os = new FileOutputStream(path+"/"+fileName); // 开始读取 while ((len = is.read(bs)) != -1) { os.write(bs, 0, len); } // 完毕,关闭所有链接 os.close(); is.close();