快精灵印艺坊 您身边的文印专家
广州名片 深圳名片 会员卡 贵宾卡 印刷 设计教程
产品展示 在线订购 会员中心 产品模板 设计指南 在线编辑
 首页 名片设计   CorelDRAW   Illustrator   AuotoCAD   Painter   其他软件   Photoshop   Fireworks   Flash  

 » 彩色名片
 » PVC卡
 » 彩色磁性卡
 » 彩页/画册
 » 个性印务
 » 彩色不干胶
 » 明信片
   » 明信片
   » 彩色书签
   » 门挂
 » 其他产品与服务
   » 创业锦囊
   » 办公用品
     » 信封、信纸
     » 便签纸、斜面纸砖
     » 无碳复印纸
   » 海报
   » 大篇幅印刷
     » KT板
     » 海报
     » 横幅

处理系统中的各类附件,上传下载


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

    }

}




返回类别: 教程
上一教程: java中有关日期的显示问题
下一教程: J2SE5.0实例---注释(annotation)

您可以阅读与"处理系统中的各类附件,上传下载"相关的教程:
· JSPSMARTUPLOAD上传下载全攻略1
· 文件上传下载
· 上传下载全攻略JSPSMARTUPLOAD
· JSPSMARTUPLOAD上传下载全攻略2
· java做的比较完善的FTP连接上传下载文件
    微笑服务 优质保证 索取样品