@Override public void onCompleted(ConvertTask task) { SharedFile sf = this.taskTagFileMap.get(task.getTaskTag()); if (null == sf) { return; } Logger.d( this.getClass(), " ConvertTask: taskTag: " + task.getTaskTag() + " state : " + task.getStateCode().getDescription() + " " + " FileURIList : " + task.getConvertedFileURIList().toString()); List<String> uriList = task.getConvertedFileURIList(); // 组装图片访问 URL for (String uri : uriList) { String url = this.fileUrlPrefix + uri; sf.appendUrl(url); } sf.getHttpSession().setAttribute("file", sf.toJSON()); sf.getHttpSession().setAttribute("convertstate", "completed"); this.taskTagFileMap.remove(task.getTaskTag()); }
@Override public void onExecuting(ConvertTask task) { SharedFile sf = this.taskTagFileMap.get(task.getTaskTag()); if (null != sf) { Logger.d(this.getClass(), " ConvertTask: " + task.getTaskTag() + " Executing"); sf.getHttpSession().setAttribute("convertstate", "executing"); } }
protected String startConverting(SharedFile file) { Logger.d(this.getClass(), "startConverting:" + file.getAliasFile().getAbsolutePath()); ConvertTask task = new ConvertTask(file.getAliasFile().getAbsolutePath(), this.workPath); taskTagFileMap.put(task.getTaskTag(), file); ConvertTool.getInstance().addConvertTask(task); return task.getTaskTag(); }
/** * Closes this input stream and releases any system resources associated with the stream. * * @exception IOException if an I/O error occurs. */ public void close() throws IOException { if (in == null) return; try { if (master) sf.forceClose(); else sf.close(); } finally { sf = null; in = null; buf = null; } }
private SharedFile existSharedFile(String filename) { SharedFile sharedFile = null; if (!this.sharedFileList.isEmpty()) { for (SharedFile sf : this.sharedFileList) { if (sf.getOriginFile().getName().equals(filename)) { sharedFile = sf; break; } } } return sharedFile; }
private void init(SharedFile sf, int size) throws IOException { this.sf = sf; this.in = sf.open(); this.start = 0; this.datalen = in.length(); // XXX - file can't grow this.bufsize = size; buf = new byte[size]; }
/** Used internally by the <code>newStream</code> method. */ private SharedFileInputStream(SharedFile sf, long start, long len, int bufsize) { super(null); this.master = false; this.sf = sf; this.in = sf.open(); this.start = start; this.bufpos = start; this.datalen = len; this.bufsize = bufsize; buf = new byte[bufsize]; }
@Override public void onTaskFailed(ConvertTask task, StateCode code) { // TODO 转换失败 SharedFile sf = this.taskTagFileMap.get(task.getTaskTag()); if (null == sf) { // TODO Error return; } Logger.d( this.getClass(), " ConvertTask: " + task.getTaskTag() + " Failed : " + code.getDescription() + " FailCode: " + task.getFaileCode() + " FileURIList : " + task.getConvertedFileURIList().toString()); sf.getHttpSession().setAttribute("convertstate", "failed"); this.taskTagFileMap.remove(task.getTaskTag()); }
/** * 接收文件数据。 * * @param name * @param request */ public void receive(String name, HttpServletRequest request) { ServletFileUpload upload = new ServletFileUpload(this.factory); upload.setSizeMax(this.fileSizeMax); upload.setHeaderEncoding("UTF-8"); // 设置文件上传进度监听器 FileProgressListener pl = new FileProgressListener(name, request.getSession()); upload.setProgressListener(pl); try { List<FileItem> items = upload.parseRequest(request); for (FileItem item : items) { if (!item.isFormField()) { // 原始文件名 String originFileName = item.getName(); // 文件后缀名 String extension = FileUtils.extractFileExtension(originFileName); // 文件别名 String aliasFileName = Utils.randomString(32) + "." + extension; FileType fileType = FileType.parseType(extension); if (FileType.UNKNOWN != fileType) { // 存储路径 String strFilePath = this.storePath + name + "/"; // 创建工作目录 File filePath = new File(this.workPath + name + "/"); if (!filePath.exists()) { filePath.mkdirs(); } // 创建存储目录 filePath = new File(strFilePath); if (!filePath.exists()) { filePath.mkdirs(); } filePath = null; // 删除相同文件 SharedFile sf = this.existSharedFile(originFileName); if (null != sf) { sf.delete(); this.removeSharedFile(sf); } // 原文件 File originFile = new File(strFilePath, originFileName); // 别名文件 File aliasFile = new File(this.workPath + name + "/", aliasFileName); sf = new SharedFile(name, originFile, aliasFile, fileType, request.getSession()); // 添加分享文件 this.addSharedFile(sf); synchronized (pl.files) { pl.files.add(sf); } // 写文件 item.write(originFile); request.getSession().setAttribute("name", name); request.getSession().setAttribute("filename", originFileName); } else { // 不支持的类型 Logger.w(this.getClass(), "不支持的文件类型: " + originFileName); request.getSession().setAttribute("name", name); request.getSession().setAttribute("filename", originFileName); request.getSession().setAttribute("state", 300); } } } // 结束 pl.finish(); } catch (FileUploadException e) { Logger.log(this.getClass(), e, LogLevel.ERROR); } catch (Exception e) { Logger.log(this.getClass(), e, LogLevel.ERROR); } }