您的位置:首页 > 其它

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

2005-04-15 14:56 344 查看
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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: