處理系統中的各類附件,上傳下載

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();

    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章