📄 uploadfile.java
字号:
package com.trulytech.mantis.util;
import com.jspsmart.upload.*;
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import com.trulytech.mantis.system.*;
import java.util.ArrayList;
/**
* <p>Title: UploadFile</p>
* <p>Description: 文件上传</p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author WangXian
* @version 1.2
*/
public class UploadFile {
public static long id = 0;
/**
* 上传文件
* @param request 请求
* @param mySmartUpload Upload对象
* @return int 文件上传个数
* @throws Exception
*/
public static int Upload(HttpServletRequest request,
SmartUpload mySmartUpload) throws
Exception {
//写日志
int uploadCount = 0;
logWriter.Info("开始上传文件 ");
uploadCount = mySmartUpload.save(request.getSession().getServletContext().
getRealPath("/") +
System.getProperty("file.separator") +
com.trulytech.mantis.system.Properties.
UploadPath);
return uploadCount;
}
/**
* 上传文件(生成唯一文件名)
* @param request 请求
* @param mySmartUpload Upload对象
* @return int 文件上传个数
* @throws Exception
*/
public static ArrayList UploadEx(HttpServletRequest request,
SmartUpload mySmartUpload) throws Exception {
//写日志
logWriter.Info("开始上传文件 ");
ArrayList Vec = new ArrayList();
String suffix = new String();
String trace = new String();
Request req = mySmartUpload.getRequest();
//循环取得所有上载的文件
for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) {
changeID();
//取得上载的文件
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
if (!myFile.isMissing()) {
//取得上载的文件的文件名
String myFileName = myFile.getFileName();
//取得不带后缀的文件名
if (myFileName.lastIndexOf('.') <= 0)
suffix = myFileName;
else
suffix = myFileName.substring(0, myFileName.lastIndexOf('.'));
//取得后缀名
String ext = mySmartUpload.getFiles().getFile(0).getFileExt();
//取得文件的大小
int fileSize = myFile.getSize();
//保存路径
if (ext.equalsIgnoreCase("")) {
trace = request.getSession().getServletContext().
getRealPath("/") + System.getProperty("file.separator") +
com.trulytech.mantis.system.Properties.UploadPath +
System.getProperty("file.separator") + suffix +
String.valueOf(System.currentTimeMillis()) + String.valueOf(id);
myFile.saveAs(trace, com.jspsmart.upload.File.SAVEAS_AUTO);
Vec.add(suffix +
String.valueOf(System.currentTimeMillis() + String.valueOf(id)));
}
else {
trace = request.getSession().getServletContext().
getRealPath("/") + System.getProperty("file.separator") +
com.trulytech.mantis.system.Properties.UploadPath +
System.getProperty("file.separator") + suffix +
String.valueOf(System.currentTimeMillis()) + String.valueOf(id) +
"." + ext;
myFile.saveAs(trace, com.jspsmart.upload.File.SAVEAS_AUTO);
Vec.add(suffix + String.valueOf(System.currentTimeMillis()) +
String.valueOf(id) + "." +
ext);
}
}
}
return Vec;
}
/**
* 上传单个文件
* @return String 如果为"",则上传失败
* @param request HttpServletRequest
* @param Path String 存储相对路径
* @param FileName String 文件名
* @param mySmartUpload SmartUpload
* @throws Exception
*/
public static String UploadSingle(HttpServletRequest request, String Path,
String FileName,
SmartUpload mySmartUpload) throws Exception {
//写日志
logWriter.Info("开始上传文件 ");
String trace = new String();
Request req = mySmartUpload.getRequest();
//循环取得所有上载的文件
//取得上载的文件
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
if (!myFile.isMissing()) {
//取得上载的文件的文件名
String myFileName = myFile.getFileName();
//取得不带后缀的文件名
//取得后缀名
String ext = mySmartUpload.getFiles().getFile(0).getFileExt();
//取得文件的大小
int fileSize = myFile.getSize();
//保存路径
if (ext.equalsIgnoreCase("")) {
trace = request.getSession().getServletContext().
getRealPath("/") + System.getProperty("file.separator") +
Path +
System.getProperty("file.separator") + FileName;
myFile.saveAs(trace, com.jspsmart.upload.File.SAVEAS_AUTO);
return FileName;
}
else {
trace = request.getSession().getServletContext().
getRealPath("/") + System.getProperty("file.separator") +
Path +
System.getProperty("file.separator") + FileName +
"." + ext;
myFile.saveAs(trace, com.jspsmart.upload.File.SAVEAS_AUTO);
return FileName + "." + ext;
}
}
else
return "";
}
/**
* 增加ID
*/
private static synchronized void changeID() {
id++;
if (id > 99999999)
id = 0;
}
/**
* 下载指定文件
* @param SrcFile String 源文件
* @param config ServletConfig
* @param request HttpServletRequest
* @param response HttpServletResponse
* @throws Exception
*/
public static void DownLoad(String SrcFile, ServletConfig config,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
try {
logWriter.Info("开始下载文件 " + SrcFile);
com.jspsmart.upload.SmartUpload mySmartUpload = new com.jspsmart.upload.
SmartUpload();
mySmartUpload.initialize(config, request, response);
mySmartUpload.downloadFile(SrcFile);
}
catch (Exception e) {
if (e instanceof java.io.FileNotFoundException)
throw e;
}
}
/**
* 用另外的文件名下载文件
* @param SrcFile String
* @param ContentType String
* @param autoOpen boolean 是否自动打开
* @param DescFile String
* @param config ServletConfig
* @param request HttpServletRequest
* @param response HttpServletResponse
* @throws Exception
*/
public static void DownLoad(String SrcFile, String ContentType,
String DescFile, boolean autoOpen,ServletConfig config,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
try {
logWriter.Info("开始下载文件 " + SrcFile);
com.jspsmart.upload.SmartUpload mySmartUpload = new com.jspsmart.upload.
SmartUpload();
mySmartUpload.initialize(config, request, response);
//是否自动打开
if (!autoOpen)
mySmartUpload.setContentDisposition(null);
mySmartUpload.downloadFile(SrcFile, ContentType,
new String(DescFile.getBytes("GB2312"),
"ISO8859_1"));
}
catch (Exception e) {
if (e instanceof java.io.FileNotFoundException)
throw e;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -