@RequestMapping(value = "/officeSingleToOfdByFileId", method = RequestMethod.POST) public ResponseData officeSingleToOfdByFileId(HttpServletRequest request, HttpServletResponse response) throws Exception { Map map=new HashMap(); String fileId=request.getParameter("fileId"); map.put("oldFileId",fileId); String newUrul="";//访问地址 JSONObject obj=fileStoreInfoManager.queryOfdFile(map);//查询该文件是否转过ofd文件 if(obj==null){ File file=null; Boolean bool=false;//用来判断是否是大文件预览 File afterFile=null; String path = request.getSession().getServletContext().getRealPath("")+ File.separator+ "newofd"+File.separator+fileId+".ofd"; //转换为ofd文件并入库 try{ InputStream inputStream =FileUploadUtil.getInputStreamFromFileServer(fileId); FileOutputStream fileOutputStream=new FileOutputStream(path); //获取当前查看文件的信息 map.put("fileId",fileId); FileStoreInfo fileStoreInfoDatabase=fileStoreInfoManager.queryFile(map).toJavaObject(FileStoreInfo.class); file=FileUploadUtil.getFile(fileStoreInfoDatabase,inputStream,request,fileId);//流获取文件 if(file!=null){ OldNewfileinfo oldNewfileinfo=new OldNewfileinfo(); oldNewfileinfo.setRecid(UuidOpt.getUuidAsString32()); oldNewfileinfo.setOdlfileid(fileId); oldNewfileinfo.setSummarys("office文件类型为"+fileStoreInfoDatabase.getFileType()+"转ofd"); oldNewfileinfo.setCreatetime(new Date()); //小于2M 直接转换成ofd文件 if(file.length()<2*1048576){ //小文件提前保存是为了防止文件正在转换,用户关闭页面重新打开 oldNewfileinfoManager.saveNewObject(oldNewfileinfo); newUrul=getNewUrul(oldNewfileinfo,path,file,fileOutputStream,fileStoreInfoDatabase,inputStream,false); }else{//大于50M的文件通过线程池进行转换上传,上传完处理存入库中 bool=true; File fileNew=file; String pathNew=path; oldNewfileinfo.setIsBigFile("0"); oldNewfileinfoManager.saveNewObject(oldNewfileinfo); //使用多线程技术,提高程序的效率 有一个客户端上传文件,就开启一个线程,完成文件的上传 ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor( 5, 20, 5000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(5), new NamedThreadFactory("thread-pool", false), new ThreadPoolExecutor.AbortPolicy()); threadPoolExecutor.submit(new Runnable() {// @SneakyThrows @Override public void run() { getNewUrul(oldNewfileinfo,pathNew,fileNew,fileOutputStream,fileStoreInfoDatabase,inputStream,true); } }); threadPoolExecutor.shutdown(); newUrul="0"; } } } catch (NullPointerException e) { LOG.info("空指针异常"); } catch (Exception e) { System.out.println(e.getMessage()); System.out.println("文件转换出错"); }finally { if(file!=null){ if(!bool){ file.delete(); } } if(afterFile!=null){ if(!bool){ afterFile.delete(); } } } }else{ //如果是大文件类型,并且还没有查询除结果,则返回前台页面url为0 if(String.valueOf(obj.get("isbigfile")).equals("0") && obj.get("newFileId")==null){ newUrul="0"; }else{ //如果转换过无需在转换,直接查看 newUrul=obj.get("newFileId")==null?"0":String.valueOf(obj.get("newFileId")); } } return ResponseData.makeResponseData(newUrul); } /** * @Author qi_xq * @Description 办公文件转ofd文件,并且将ofd文件上传至文件服务器并且 * @Date 9:01 2022/6/8 * @Param [oldNewfileinfo, path, file, fileOutputStream, fileStoreInfoDatabase, inputStream] * @return java.lang.String */ public String getNewUrul(OldNewfileinfo oldNewfileinfo,String path,File file,FileOutputStream fileOutputStream,FileStoreInfo fileStoreInfoDatabase,InputStream inputStream,Boolean bool)throws Exception{ String newUrul=""; try { File afterFile=new File(path); String syText=CodeRepositoryUtil.getValue("SYSZ","A"); ha.addTextMark(file,fileOutputStream,new TextInfo(syText,"宋体", 18,"#80000",45,Const.XAlign.Center,Const.YAlign.Middle), new MarkPosition(100f, 200f, 100f, 50f, MarkPosition.INDEX_ALL),true,true); //上传文件服务器(转换之后的文件) FileStoreInfo fileStoreInfo=new FileStoreInfo(); //获取原服务器文件基本信息 fileClient=FileUploadUtil.getFileClient(); fileStoreInfo.setFileName(fileStoreInfoDatabase.getFileName().substring(0,fileStoreInfoDatabase.getFileName().lastIndexOf("."))+".ofd");//设置文件名称 fileStoreInfo.setFileType(".ofd");//设置文件类型 //上传文件服务器(转换之后的ofd文件) fileStoreInfo= fileClient.uploadFile(fileClient.allocHttpClient(),fileStoreInfo,afterFile); oldNewfileinfo.setNewfileid(fileStoreInfo.getFileId()); oldNewfileinfo.setContents(downFilePath+"/"+oldNewfileinfo.getNewfileid()+CodeRepositoryUtil.getSysConfigValue("reader")); oldNewfileinfo.setSummarys("office文件类型为"+fileStoreInfoDatabase.getFileType()+"转ofd"); oldNewfileinfo.setCreatetime(new Date()); oldNewfileinfoManager.updateObject(oldNewfileinfo); newUrul=fileStoreInfo.getFileId()==null?"":fileStoreInfo.getFileId(); inputStream.close(); fileOutputStream.close(); file.delete(); } catch (Exception e) { e.printStackTrace(); logger.info(e.getMessage()); } return newUrul; }
第二种线程方式
@RequestMapping(value = {"/toExportFile"},method = {RequestMethod.GET}) public void toExportFile(HttpServletRequest request, HttpServletResponse response) throws Exception { CentitUserDetails ud = getLoginUser(request); String userCode = ud.getUserCode(); // 用户登录超时,跳转到登陆页 if (ud == null) { JsonResultUtils.writeErrorMessageJson("登录失效", response); }else { Map<String, Object> map = convertSearchColumn(request); map.put("idStr", request.getParameter("idStr")); map.put("dowTime", request.getParameter("dowTime")); System.out.println("**map**"+map); new Thread() { @Override public void run() { String m = ""; try { map.put("tempPath", servletContext.getRealPath(File.separator + "temporaryFile") + File.separator + userCode+map.get("dowTime")); map.put("temporaryZipFile", servletContext.getRealPath(File.separator + "temporaryZipFile") + File.separator + userCode+map.get("dowTime")); map.put("tempPathWJJ", servletContext.getRealPath(File.separator + "temporaryFile") + File.separator); map.put("temporaryZipFileWJJ", servletContext.getRealPath(File.separator + "temporaryZipFile") + File.separator); map.put("path", servletContext.getRealPath("")); System.out.println("**map2**"+map); //CommUtil.delAllFiles(new File(map.get("tempPath").toString()), null); //CommUtil.delAllFiles(new File(map.get("temporaryZipFile").toString() + File.separator), null); dowExportFile(map, request, response); } catch (Exception e) { m = e.getMessage(); System.out.println(m); System.out.println(e); } } }.start(); JsonResultUtils.writeSingleDataJson("开始处理压缩包", response); } }
标签:map,java,String,----,线程,File,new,null,oldNewfileinfo From: https://www.cnblogs.com/qxqbk/p/17011803.html