java上传下载基类

package com.pl.skh.utils.tools;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import com.jspsmart.upload.File;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.pl.skh.common.vo.PLFile;
import com.pl.skh.common.vo.ResultMessage;

/**
 * 
 * 文件处理类
 * 
 * @author bin
 * 
 */
public class UploadTool {
// 上传图片文件类型
private final String ALLOWED_IMAGE = "jpg,jpeg,bmp,gif";


// 上传文本文件类型
private final String ALLOWED_TEXT = "doc,txt,pdf,ppt,rar,xls";


/**
* 上传图片

* @param smartUpload
*            SmartUpload Smart对象
* @param dir
*            String 目录
* @return ArrayList 文件名称列表
*/
public ArrayList uploadImage(SmartUpload smartUpload, String dir,
ResultMessage resultMessage) {
return this.uploadFile(smartUpload, this.ALLOWED_IMAGE, dir,
resultMessage);
}


/**
* 上传文本

* @param smartUpload
*            SmartUpload Smart对象
* @param dir
*            String 目录
* @return ArrayList 文件名称列表
*/
public ArrayList uploadText(SmartUpload smartUpload, String dir,
ResultMessage resultMessage) {
return this.uploadFile(smartUpload, this.ALLOWED_TEXT, dir,
resultMessage);
}


/**
* 上传文件

* @param smartUpload
*            SmartUpload Smart对象
* @param smartUpload
*            String 文件类型
* @param dir
*            String 目录
* @return ArrayList 文件名称列表
*/


@SuppressWarnings("unchecked")
public ArrayList uploadFile(SmartUpload smartUpload, String allowedFiles,
String dir, ResultMessage resultMessage) {
ArrayList list = new ArrayList();
try {
// 限制每个上传文件的最大长度
smartUpload.setMaxFileSize(1024 * 1024);
// 限制总上传数据的长度
smartUpload.setTotalMaxFileSize(5 * 1024 * 1024);
// 设定允许上传的文件(通过扩展名限制)
smartUpload.setAllowedFilesList(allowedFiles);
// 设定禁止上传的文件(通过扩展名限制)
smartUpload.setDeniedFilesList("exe,bat,jsp,htm,html,,");
// 上传文件,此项是必须的
smartUpload.upload();
// 统计上传文件的总数
int count = smartUpload.getFiles().getCount();
// 取得Request对象
Request myRequest = smartUpload.getRequest();
String rndFilename, fileExtName, fileName, memo;
Date dt = null;
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");


// 逐一提取上传文件信息,同时可保存文件
for (int i = 0; i < count; i++) {
PLFile plsFile = new PLFile();
// 取得一个上传文件
File file = smartUpload.getFiles().getFile(i);
// 若文件不存在则继续
if (file.isMissing()) {
continue;
}
// 取得文件名
fileName = file.getFileName();
// 取得文件扩展名
fileExtName = file.getFileExt();
// 取得随机文件名
dt = new Date(System.currentTimeMillis());
Thread.sleep(100);
rndFilename = fmt.format(dt) + "." + fileExtName;
memo = myRequest.getParameter("memo" + i);


// 将文件另存,以WEB应用的根目录作为上传文件的根目录
file.saveAs(dir + rndFilename, SmartUpload.SAVE_VIRTUAL);
plsFile.setFileName(fileName);
plsFile.setDescFileName(dir + rndFilename);
plsFile.setMemo(memo);
list.add(plsFile);
}
System.out.println(count + "个文件上传成功!<br>");
} catch (Exception ex) {
// System.out.println("上传文件超过了限制条件,上传失败!<br>");
resultMessage.setMsg("上传文件超过了限制条件,上传失败!");
// System.out.println("错误原因:<br>" + ex.toString());
}
return list;
}


/**
* 下载文件

* @param smartUpload
*            SmartUpload SmartUpload对象
* @param fileName
*            String 文件名
* @param fileType
*            String 文件类型
* @param descFileName
*            String 存储文件名
*/
public void downloadFile(SmartUpload smartUpload, String fileName,
String fileType, String descFileName) {
try {
// 设定contentDisposition为null以禁止浏览器自动打开文件,
smartUpload.setContentDisposition(null);
// 下载文件


// 解决下载文件的中文问题
byte[] b = descFileName.getBytes();
char[] c = new char[b.length];
for (int x = 0; x < b.length; x++) {
c[x] = (char) (b[x] & 0x00FF);
}
descFileName = new String(c);
smartUpload.downloadFile(fileName, fileType, descFileName);
} catch (Exception ex) {
System.out.println("下载文件失败!<br>");
System.out.println("下载文件失败!<br>");
System.out.println("错误原因:<br>" + ex.toString());
}
}


/**
* 取文件扩展名
*/
public static String getExtension(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int i = filename.lastIndexOf('.');


if ((i > -1) && (i < (filename.length() - 1))) {
return filename.substring(i + 1);
}
}
return filename;
}


/**
* 取文件名
*/
public static String getFileName(String leftAlphaBeta,
String rightAlphaBeta, String fileAllPath) {
String fileName = fileAllPath;
if ((fileAllPath != null) && (fileAllPath.length() > 0)) {
int leftIndex = 0;
int rightIndex = fileAllPath.length();
if (leftAlphaBeta.length() > 0) {
leftIndex = fileAllPath.lastIndexOf(leftAlphaBeta);
}
if (rightAlphaBeta.length() > 0) {
rightIndex = fileAllPath.lastIndexOf(rightAlphaBeta);
}


if ((leftIndex > -1) && (leftIndex < (fileAllPath.length() - 1))
&& (rightIndex > -1)
&& (rightIndex) < (fileAllPath.length() - 1)
&& leftIndex < rightIndex) {
fileName = fileAllPath.substring(leftIndex + 1, rightIndex);
}
}
return fileName;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值