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指定的路径下。 * <p>title: objective management system</p> * <p>description: </p> * <p>copyright: copyright (c) 2004</p> * @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"); }
} else { string filename = for_upload.getfilename(); saved = new java.io.file(dir.getabsolutepath() + java.io.file.separator + filename); } if (saved == null) { return null; } try { for_upload.saveas(saved.getabsolutepath(), smartupload.save_physical); } catch (exception ex) { ex.printstacktrace(); log.debug(ex, "filekeeper"); saved = null; } if(saved!=null){ return relative_dir + saved.getname(); }else{ return null; } }
/** * 取得一个指定文件的流。 * @param relative_path string 相对路径,包含文件名。 * @return inputstream 该文件的输入流,供外部程序读取。 */ public static inputstream download(string relative_path) { if (!relative_path.startswith("\\")) { relative_path = "\\" + relative_path; } java.io.file file = new java.io.file(base_dir + relative_path); if (!file.exists()) { return null; } else { try { return new fileinputstream(file); } catch (exception ex) { ex.printstacktrace(); log.debug(ex, "filekeeper"); return null; }
}
} /** * 删除指定文件。 * @param relative_path string 文件的相对路径。请不要以“/”开头。可以用"\"开头,也可以不用。 */ public static void delete(string relative_path){ if(!relative_path.startswith("\\")){ relative_path = "\\"+relative_path; } java.io.file file = new java.io.file(base_dir+relative_path); if(file.exists() && file.isfile()){ file.delete(); }
}
public void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { //system.out.println("do post…."); doget(request, response); } /** * 处理下载文件的请求。需要在request中提供三个参数: * 1.path,说明需要下载的文件的相对路径,包含磁盘文件名本身。 * 2.filename,说明一个文件名,这个文件名将成为用户保存文件时的默认用户名。如果不提供,系统取在path中的文件名 * 3.mime,说明文件的mime_type。如果不提供,默认为"application/*"。 * @param request httpservletrequest * @param response httpservletresponse * @throws servletexception * @throws ioexception */ public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { //system.out.println("filekeeper do get…");
string relative_path = parameterparser.getstrpara(request, "path"); string filename = parameterparser.getstrpara(request, "filename"); string mime = parameterparser.getstrpara(request, "mime");
if (mime.length() > 0) { response.setcontenttype(mime); } else { response.setcontenttype("application/*"); } response.setheader("content-disposition", "attachment;filename=" + filename);
inputstream in = download(relative_path);
if (in == null) { log.debug("文件" + filename + "不存在.", this); response.getoutputstream().close(); return; } byte[] b = new byte[1024]; int len; while ( (len = in.read(b)) > 0) { response.getoutputstream().write(b, 0, len); } in.close(); response.getoutputstream().flush(); response.getoutputstream().close();
}
}
