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. *;
/ ** * Handling various accessories in the system. These attachments are saved to the path specified in AttachmentPath in Config.properties. *
Title: Objective Management System p> *
Description: 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);}}
/ ** * Upload a file and save it to the specified folder. * @Param for_upload file Requires saved file * @Param relative_dir string Specified folder (relative path), path "//" segmentation. * @Param Rename Boolean Do you want to automatically rename it Other name * @Return String If saved, return the relative address. Otherwise, return 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) {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.seParetor 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;}} / ** * A stream of specified files is obtained. * @Param Relative_Path String relative path, contains the file name. * @Return InputStream This file is input for external programs. * / 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;}
}
} / ** * Delete the specified file. * @Param Relative_Path String file relative path. Please do not start with "/". You can use "/" to start or not. * / 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);} / ** * process the request to download the file . There is a need to provide three parameters in the request: * 1.Path, which means the relative path of the file you need to download, contains the disk file name itself. * 2.FileName, illustrate a file name, this file name will become the default user name when the user saves files. If not provided, the system takes the file name * 3.mime in the path, indicating the MIME_TYPE of the file. If not, the default is "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 ("file" filename "does not exist.", 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 ();
}
}