package com.highcom.object.common;
import java.io.*;import javax.servlet.*;import com.jspsmart.upload.*;import com.highcom.hcgip.basic.common.*;import javax.servlet.http.*;
/** * 处理系统中的各类附件。这些附件被保存到在config.properties中attachmentpath指定的路径下。 * Title: Objective Management System * Description: * Copyright: Copyright (c) 2004 * @version 1.0 */
public class FileKeeper extends javax.servlet.http.HttpServlet { public static String base_dir; static { base_dir = PropertiesReader.getConfigValue("attachmentpath"); }
public FileKeeper() { }
public static String getRelativePath(java.io.File abs_path){ String fullpath= abs_path.getAbsolutePath(); String new_fullpath = fullpath.replaceAll("/","\\").toLowerCase(); String new_base_dir = base_dir.replaceAll("/","\\").toLowerCase(); int i=new_fullpath.indexOf(new_base_dir); if(i<0){ return fullpath; }else{ return fullpath.substring(i); } }
/** * 上传一个文件,保存到指定文件夹。 * @param for_upload File 需要保存的文件 * @param relative_dir String 指定的文件夹(相对路径),路径用"\\"分割。 * @param rename boolean 是否要系统自动重命名为其它名字 * @return String 如果保存成功,返回相对地址。否则,返回null */ public static String upload(com.jspsmart.upload.File for_upload, String relative_dir, boolean rename) { if (for_upload == null) { return null; } if (relative_dir == null || relative_dir.length() == 0) { relative_dir = "\\"; } if (!relative_dir.startsWith("\\")) { relative_dir = "\\" + relative_dir; } if (!relative_dir.endsWith("\\")) { relative_dir = relative_dir + "\\"; } java.io.File dir = new java.io.File(base_dir + relative_dir); if (!dir.exists()) { dir.mkdirs(); } java.io.File saved = null; if (rename) { try { saved = java.io.File.createTempFile("sys", "", dir); } catch (Exception ex) { ex.printStackTrace(); Log.debug(ex, "FileKeeper"); } |