文件上传下载action 和service
来源:互联网 发布:葡语翻译软件 编辑:程序博客网 时间:2024/06/06 06:46
/**
* property serialVersionUID.
*/
private static final long serialVersionUID = -3174042565144909773L;
/**
* property file.
*/
private File file; // 上传文件对象
/**
* property files.
*/
private File[] files; // 上传文件对象数组
/**
* property fileUpload.
*/
private FileUpload fileUpload; // 文件上传实体类对象
/**
* property sourceNames.
*/
private String[] sourceNames; // 原文件名称数组
/**
* property postfixs.
*/
private String[] postfixs; // 原文件后缀名数组
/**
* property fileUploadService.
*/
private FileUploadService fileUploadService; // 文件上传逻辑
/**
* property result.
*/
private String result;
/**
* 文件大小常量,1M
*/
private static final double FILE_SIZE1 = 1024.0f;
/**
* 1G常量.
*/
private static final double FILE_SIZE2 = FILE_SIZE1 * FILE_SIZE1;
/**
* 除后大小常量.
*/
private static final int CUT_SIZE = 10;
/**
* 日志.
*/
private Logger log = LoggerFactory.getLogger(FileUploadAction.class);
/**
* 显示上传文件页面.
* @return 值.
* @throws Exception 异常.
*/
public String showUploadPage() throws Exception {
SessionBean sb = getSessionBean();
String empName = sb.getEmpName();
request.setAttribute("empName", empName);
String isMany = request.getParameter("isMany");
request.setAttribute("isMany", StringUtils.isEmpty(isMany) ? false
: Boolean.valueOf(isMany));
String contextPath = request.getParameter("contextPath");
request.setAttribute("contextPath",
StringUtils.isEmpty(contextPath) ? "otherFile" : contextPath);
return SUCCESS;
}
/**
* 无用保留方法.
* @return 空.
*/
public String method1() {
return null;
}
/**
* 无用保留方法.
* @return 空.
*/
public String method2() {
return null;
}
/**
* 清空属性.
*/
private void clearProperty() {
files = null;
sourceNames = null;
postfixs = null;
file = null;
fileUpload = null;
}
/**
* 表单提交上传文件(只能上传一个).
* .
* @return null 值.
* @throws Exception 异常.
*/
public String submitFileUpload() throws Exception {
// 设置相应字符编码
response.setContentType("text/html;charset=UTF-8");
String message = "";
if (filError2()) {
message = PropertiesSon.getText("file_upload_message");
response.getWriter().print(message);
} else {
// 得到当前用户名
fileUpload.setUserOfUpload(((SessionBean) session
.getAttribute("userinfo")).getEmpName());
// 得到文件所属文件夹名称
String contextPath = request.getParameter("contextPath");
// 得到上传文件的文件夹名称 将上传用户封装到文件上传对象中
fileUpload.setContextPath(contextPath);
// 调用上传文件逻辑
FileUpload uploadFile = fileUploadService.uploadFileToServerAndDB(
file, fileUpload);
log.info("表单提交上传文件");
clearProperty();
// 回调javascript上传成功方法
message = "<script type='text/javascript'>window.parent.uploadSuccess('"
+ uploadFile.getUrl() + "');</script>";
response.getWriter().print(message);
}
return null;
}
/**
* 表单提交上传文件(只能上传一个).
* .
* @return null 值.
* @throws Exception 异常.
*/
public String submitFileUpload2() throws Exception {
// 设置相应字符编码
response.setContentType("text/html;charset=UTF-8");
String message = "";
if (filError2()) {
message = PropertiesSon.getText("file_upload_message");
response.getWriter().print(message);
} else {
// 得到当前用户名
fileUpload.setUserOfUpload(((SessionBean) session
.getAttribute("userinfo")).getEmpName());
// 得到文件所属文件夹名称
String contextPath = request.getParameter("contextPath");
// 得到上传文件的文件夹名称 将上传用户封装到文件上传对象中
fileUpload.setContextPath(contextPath);
// 调用上传文件逻辑
FileUpload uploadFile = fileUploadService.uploadFileToServerAndDB(
file, fileUpload);
log.info("表单提交上传文件");
clearProperty();
// 回调javascript上传成功方法
response.getWriter().print(
"<script type='text/javascript'>window.parent.uploadSuccess('"
+ uploadFile.getId() + "');</script>");
}
return null;
}
/**
* 表单提交上传文件(只能上传一个)带水印的文件上传.
* .
* @return null 值.
* @throws Exception 异常.
*/
public String submitFileUpload3() throws Exception {
// 设置相应字符编码
response.setContentType("text/html;charset=UTF-8");
String message = "";
if (filError2()) {
message = PropertiesSon.getText("file_upload_message");
response.getWriter().print(message);
} else {
// 得到当前用户名
fileUpload.setUserOfUpload(((SessionBean) session
.getAttribute("userinfo")).getEmpName());
// 得到文件所属文件夹名称
String contextPath = request.getParameter("contextPath");
// 得到上传文件的文件夹名称 将上传用户封装到文件上传对象中
fileUpload.setContextPath(contextPath);
// 调用上传文件逻辑
FileUpload uploadFile = fileUploadService.uploadFileToServerAndDB3(
file, fileUpload);
log.info("表单提交上传文件");
clearProperty();
JSONObject jsonObj = CommonUtil.formatObjectToJson(uploadFile);
// 回调javascript上传成功方法
response.getWriter().print(
"<script type='text/javascript'>window.parent.uploadSuccess("
+ jsonObj + ");</script>");
}
return null;
}
/**
* 播放视频.
* @return 值.
* @throws Exception 异常.
*/
public String playVideo() throws Exception {
String id = request.getParameter("id");
if (!verifyOr(id)) {
boolean flag = CommonUtil.verifyNumber(id);
if (flag) {
FileUpload fileUpload = fileUploadService
.getFileUploadById(Long.parseLong(id));
String videoUrl = fileUpload.getUrl();
String imgUrl = videoUrl.substring(0,
videoUrl.lastIndexOf(".") + 1) + "jpg";
request.setAttribute("videoUrl", videoUrl);
request.setAttribute("imgUrl", imgUrl);
}
}
return SUCCESS;
}
/**
* 批量上传文件.
* @return 值.
* @throws Exception 异常.
*/
public String submitBetchFileUpload() throws Exception {
// 得到超过20m的文件名称字符串
// 设置相应字符编码
response.setContentType("text/html;charset=UTF-8");
// 判断添加文件是否过大
// 若文件大小合法
if (fileError()) {
clearProperty();
// 若文件过大调用页面uploadMessage(names) javascript函数
response.getWriter()
.print("<script type='text/javascript'>window.parent.uploadMessage();</script>");
} else {
// 得到当前用户名
String empName = ((SessionBean) session.getAttribute("userinfo"))
.getEmpName();
// 得到文件所属文件夹名称
String contextPath = request.getParameter("contextPath");
List<FileUpload> fileUploadList = fileUploadService
.batchFileUploadToServerAndDB(files, sourceNames, postfixs,
contextPath, empName);
List<JSONObject> fileUploadJsonList = CommonUtil
.convertObjectListToJsonList(fileUploadList);
// 调用页面uploadSuccess(jsonList) javascript函数
response.getWriter().print(
"<script type='text/javascript'>window.parent.parent.uploadSuccess('"
+ fileUploadJsonList
+ "');window.parent.disLoadImg('" + sumFileSize()
+ "');</script>");
clearProperty();
log.info("批量上传文件");
}
// 返回Null防止页面跳转
return null;
}
/**
* 下载文件.
* @return 值.
* @throws Exception 异常.
*/
public String downloadFile() throws Exception {
if (fileUpload == null || fileUpload.getId() == null) {
return null;
}
// 根据编号查询文件上传对象
fileUpload = fileUploadService.getFileUploadById(fileUpload.getId());
if (fileUpload == null) {
return null;
}
// 设置参数准备下载文件
// 设置响应类型
response.setContentType("application/x-download");
// 原文件名称
String fileName = fileUpload.getSourceName() + "."
+ fileUpload.getPostfix();
// 设置文件名称字符编码
// fileName = URLEncoder.encode(fileName, "UTF-8");
fileName = new String(fileName.getBytes("GBK"),
"ISO8859-1");
// 设置响应头
response.addHeader("Content-Disposition", "attachment;filename="
+ fileName);
// 从属性文件中得到服务器上传文件所在主目录
String path = PropertiesSon.getText("server_uploaddir");
// 得到文件所属文件夹
String filePath = fileUpload.getContextPath();
// 得到服务器文件访问路径封装到对象中
String destPath = path + File.separator + filePath;
// 得到服务器文件物理路径
String url = destPath + System.getProperty("file.separator")
+ fileUpload.getFileEditName();
log.info("下载文件url:" + url);
// 得到响应输出流对象
OutputStream output = response.getOutputStream();
// 根据文件服务器物理路径创建文件读取对象
FileInputStream fs = new FileInputStream(url);
// 输出文件
byte[] b = new byte[NumberConstant.NUM1024];
int i = 0;
while ((i = fs.read(b)) > 0) {
output.write(b, 0, i);
}
output.flush();
fs.close();
fs = null;
output.close();
output = null;
log.info("下载文件");
// null防止页面跳转
return null;
}
/**
* 验证批量添加文件.
* .
* @return null 值.
* @throws Exception 异常.
*/
public Double sumFileSize() throws Exception {
int fileCount = files.length;
Double fileSize = 0.0;
for (int i = 0; i < fileCount; i++) {
File file = files[i];
fileSize += file.length() / (FILE_SIZE2);
}
// 返回拼接好的超出20M的文件名称字符串
return fileSize;
}
/**
* 判断文件大小.
* @return 标识.
* @throws Exception 异常.
*/
public Boolean fileError() throws Exception {
int fileCount = files.length;
for (int i = 0; i < fileCount; i++) {
File file = files[i];
if (file.length() / (FILE_SIZE2) >= CUT_SIZE) {
return true;
}
}
return false;
}
/**
* 判断文件大小.
* @return 标识.
*/
public Boolean filError2() {
Double fileSize = file.length() / (FILE_SIZE2);
return fileSize >= CUT_SIZE ? true : false;
}
----------------------------------------------------------------------------------------------------------------------------------
public class FileUploadService extends BaseService {
private Logger log = LoggerFactory.getLogger(FileUploadService.class);
/**
* 序列化码.
*/
private static final long serialVersionUID = -8240266986388761771L;
/**
* 文件大小常量,1M
*/
private static final float FILE_SIZE = 1024.0f;
/**
* 异步调用视频转换类..
*/
@Autowired
private ConvertVideoUtility convertVideoUtil;
/**
* 根据编号查询文件上传对象..
* @param id 文件上传对象编号..
* @return 文件上传对象. 值.
* @throws Exception 异常.
*/
public FileUpload getFileUploadById(Long id) throws Exception {
// 根据编号查询文件上传对象封装成map集合
String sql = "SELECT * FROM T_SYS_UPLOAD_FILE WHERE UF_ID=?";
Map<String, Object> map = commonDao.getUniqueMap(sql, id);
return getFileUploadByMap(map);
}
/**
* 从Map中取出文件对象封装成文件上传对象.
* @param map 从数据库中查询出来的map.
* @return 文件上传对象 值.
* @throws Exception 异常.
*/
public FileUpload getFileUploadByMap(Map<String, Object> map) throws Exception {
if (map == null) {
return null;
}
// 从map中取出数据封装成文件上传对象
FileUpload fileUpload = new FileUpload();
fileUpload.setId(Long.parseLong(map.get("UF_ID").toString()));
fileUpload.setContextPath(map.get("CONTEXT_PATH").toString());
fileUpload.setFileEditName(map.get("FILE_EDIT_NAME").toString());
fileUpload.setFileSize(Float
.parseFloat(map.get("FILE_SIZE").toString()));
fileUpload.setMimeType(map.get("MIME_TYPE").toString());
fileUpload.setPostfix(map.get("POSTFIX").toString());
fileUpload.setProjectName(map.get("PROJECT_NAME").toString());
fileUpload.setSourceName(map.get("SOURCE_NAME").toString());
fileUpload.setUploadTime(DateUtil.parseToTimestamp(
map.get("UPLOAD_TIME").toString(), DateUtil.format1));
fileUpload.setUrl(map.get("URL").toString());
fileUpload.setUserOfUpload(map.get("USER_OF_UPLOAD").toString());
// 返回文件上传对象
return fileUpload;
}
/**
* 上传文件.
* @param file 上传的文件对象.
* @param fileUpload 文件上传实体类.
* @return 封装好的文件上传实体类 值.
* @throws Exception 异常.
*/
@Transactional(propagation = Propagation.REQUIRED)
public FileUpload uploadFileToServerAndDB(File file, FileUpload fileUpload) throws Exception {
// 封装上传文件对象并上传文件到服务器中
fileUpload = fillUpload(fileUpload, file);
// 保存上传文件对象
commonDao.save(fileUpload);
// 返回封装好的上传文件对象
return fileUpload;
}
/**
* 上传文件.
* @param file 上传的文件对象.
* @param fileUpload 文件上传实体类.
* @return 封装好的文件上传实体类 值.
* @throws Exception 异常.
*/
@Transactional(propagation = Propagation.REQUIRED)
public FileUpload uploadFileToServerAndDB3(File file, FileUpload fileUpload) throws Exception {
// 封装上传文件对象并上传文件到服务器中
fileUpload = fillUpload3(fileUpload, file);
// 保存上传文件对象
commonDao.save(fileUpload);
// 返回封装好的上传文件对象
return fileUpload;
}
/**
* 批量上传文件.
* .
* @param files 文件对象数组.
* @param sourceNames 原文件名称数组.
* @param postfixs 文件后缀数组.
* @param contextPath 文件所在服务器文件夹.
* @param empName 上传人名称.
* @return 上传文件对象集合 值.
* @throws Exception 异常.
*/
@Transactional(propagation = Propagation.REQUIRED)
public List<FileUpload> batchFileUploadToServerAndDB(File[] files,
String[] sourceNames, String[] postfixs, String contextPath,
String empName) throws Exception {
// 得到文件对象数组数量
int count = files.length;
// 准备一个新的集合封装文件上传对象
List<FileUpload> fileUploadList = new ArrayList<FileUpload>();
// 遍历文件数组
for (int i = 0; i < count; i++) {
// 封装文件上传对象数据
FileUpload fileUpload = new FileUpload();
fileUpload.setContextPath(contextPath);
fileUpload.setUserOfUpload(empName);
fileUpload.setPostfix(postfixs[i]);
fileUpload.setSourceName(sourceNames[i]);
fileUpload = fillUpload(fileUpload, files[i]);
fileUploadList.add(fileUpload);
}
// 批量保存文件上传对象
commonDao.saveOrUpdateAll(fileUploadList);
// 返回文件上传对象集合
return fileUploadList;
}
/**
* 批量上传文件(加入containID与containType后).
* @param files 文件对象数组.
* @param sourceNames 原文件名称数组.
* @param postfixs 文件后缀数组.
* @param contextPath 文件所在服务器文件夹.
* @param empName 上传人名称.
* @param containId 用到此文件的表ID.
* @param containType 用到此文件的类型.
* @return 上传文件对象集合 值.
* @throws Exception 异常.
*/
@Transactional(propagation = Propagation.REQUIRED)
public List<FileUpload> batchFileUpload(File[] files,
String[] sourceNames, String[] postfixs, String contextPath,
String empName, Long containId, String containType) throws Exception {
// 得到文件对象数组数量
int count = files.length;
// 准备一个新的集合封装文件上传对象
List<FileUpload> fileUploadList = new ArrayList<FileUpload>();
// 遍历文件数组
for (int i = 0; i < count; i++) {
// 封装文件上传对象数据
FileUpload fileUpload = new FileUpload();
fileUpload.setContextPath(contextPath);
fileUpload.setContainId(containId);
fileUpload.setContainType(containType);
fileUpload.setUserOfUpload(empName);
fileUpload.setPostfix(postfixs[i]);
fileUpload.setSourceName(sourceNames[i]);
fileUpload = fillUpload(fileUpload, files[i]);
fileUploadList.add(fileUpload);
}
// 批量保存文件上传对象
commonDao.saveOrUpdateAll(fileUploadList);
// 返回文件上传对象集合
return fileUploadList;
}
/**
* 准备文件上传数据并传输..
* @param fileUpload 上传文件对象..
* @param file 文件对象..
* @return 文件上传对象. 值.
* @throws Exception 异常.
*/
private FileUpload fillUpload(FileUpload fileUpload, File file) throws Exception {
// 准备封装数据
// 准备文件上传对象准备封装
FileUpload newFileUpload = new FileUpload();
// 从属性文件中得到服务器上传文件所在主目录
String path = PropertiesSon.getText("server_uploaddir");
// 得到文件所属文件夹
String filePath = fileUpload.getContextPath();
// 得到服务器文件访问路径封装到对象中
String destPath = path + File.separator + filePath;
// 根据文件路径和文件主目录去服务器端创建目录
FileUtil.mkDirForFilePath(destPath);
// 得到当前时间戳并封装到文件上传对象中
Timestamp uploadTime = new Timestamp(DateUtil.curentDate().getTime());
// 得到文件mime_type
String mimeType = new MimetypesFileTypeMap().getContentType(file);
// 封装
newFileUpload.setUploadTime(uploadTime);
// 将原文件名称封装到对象中
newFileUpload.setSourceName(fileUpload.getSourceName());
// 将文件后缀封装到对象中
newFileUpload.setPostfix(fileUpload.getPostfix());
// 将文件访问路径封装到对象中
newFileUpload.setUrl(destPath);
newFileUpload.setContextPath(filePath);
// 将文件大小已KB为单位的值封装到对象中
newFileUpload.setFileSize(file.length() / FILE_SIZE);
// 将文件所属项目名称封装到对象中
newFileUpload.setProjectName("PPS_OA");
// 将上传人名称封装到对象中
newFileUpload.setUserOfUpload(fileUpload.getUserOfUpload());
// 将使用此文件的ID封装到对象中
newFileUpload.setContainId(fileUpload.getContainId());
// 将用到此文件的类型封装到对象中
newFileUpload.setContainType(fileUpload.getContainType());
// 给文件重新命名并封装到对象中
newFileUpload.setFileEditName(FileUtil.getfileName(
newFileUpload.getSourceName(), newFileUpload.getPostfix()));
// 将文件访问路径封装到对象中
String url = PropertiesSon.getText("server_accessdir")
+ newFileUpload.getContextPath() + "/"
+ CommonUtil.getURLEncode(newFileUpload.getFileEditName()
.substring(0, newFileUpload.getFileEditName().lastIndexOf("_")), "GBK")
+ newFileUpload.getFileEditName().substring(newFileUpload.getFileEditName().lastIndexOf("_"));
newFileUpload.setUrl(url);
// 得到文件的mime_type并封装到对象中
newFileUpload.setMimeType(mimeType);
// 将原文件copy到服务器路径中
FileUtil.copyFile(file,
new File(destPath + "/" + newFileUpload.getFileEditName()));
// 返回上传好的文件上传对象
return newFileUpload;
}
/**
* 准备文件上传数据并传输.
* @param fileUpload 上传文件对象.
* @param file 文件对象.
* @return 文件上传对象 值.
* @throws Exception 异常.
*/
private FileUpload fillUpload3(FileUpload fileUpload, File file) throws Exception {
// 准备封装数据
// 准备文件上传对象准备封装
FileUpload newFileUpload = new FileUpload();
// 从属性文件中得到服务器上传文件所在主目录
String path = PropertiesSon.getText("server_uploaddir");
// 得到文件所属文件夹
String filePath = fileUpload.getContextPath();
// 得到服务器文件访问路径封装到对象中
String destPath = path + File.separator + filePath;
// 根据文件路径和文件主目录去服务器端创建目录
FileUtil.mkDirForFilePath(destPath);
// 得到当前时间戳并封装到文件上传对象中
Timestamp uploadTime = new Timestamp(DateUtil.curentDate().getTime());
// 得到文件mime_type
String mimeType = new MimetypesFileTypeMap().getContentType(file);
// 封装
newFileUpload.setUploadTime(uploadTime);
// 将原文件名称封装到对象中
newFileUpload.setSourceName(fileUpload.getSourceName());
// 将文件后缀封装到对象中
newFileUpload.setPostfix(fileUpload.getPostfix());
// 将文件访问路径封装到对象中
newFileUpload.setUrl(destPath);
newFileUpload.setContextPath(filePath);
// 将文件大小已KB为单位的值封装到对象中
newFileUpload.setFileSize(file.length() / FILE_SIZE);
// 将文件所属项目名称封装到对象中
newFileUpload.setProjectName("PPS_OA");
// 将上传人名称封装到对象中
newFileUpload.setUserOfUpload(fileUpload.getUserOfUpload());
// 给文件重新命名并封装到对象中
newFileUpload.setFileEditName(FileUtil.getfileName(
newFileUpload.getSourceName(), newFileUpload.getPostfix()));
// 将文件访问路径封装到对象中
String url = PropertiesSon.getText("server_accessdir")
+ newFileUpload.getContextPath() + "/"
+ CommonUtil.getURLEncode(newFileUpload.getFileEditName()
.substring(0, newFileUpload.getFileEditName().lastIndexOf("_")), "GBK")
+ newFileUpload.getFileEditName().substring(newFileUpload.getFileEditName().lastIndexOf("_"));
newFileUpload.setUrl(url);
// 得到文件的mime_type并封装到对象中
newFileUpload.setMimeType(mimeType);
// 将原文件copy到服务器路径中
FileUtil.copyFile(file,
new File(destPath + "/" + newFileUpload.getFileEditName()));
// 返回上传好的文件上传对象
return newFileUpload;
}
//上传文件最大大小
/**
* property fileMaxSize.
*/
private static final Long FILE_MAX_SIZE = 20971520L;
/**
* @param request 请求对象.
* @param fileUpload 文件上传对象.
* @return JsonMessage 封装对象 值.
* @throws IOException 文件IO异常.
* @throws Exception 异常.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Transactional(propagation = Propagation.REQUIRED)
public JsonMessage saveFile(HttpServletRequest request,
FileUpload fileUpload) throws IOException, Exception {
JsonMessage jm = new JsonMessage();
jm.setIsSuccess(false);
// 得到当前时间戳并封装到文件上传对象中
Timestamp uploadTime = new Timestamp(DateUtil.curentDate().getTime());
fileUpload.setUploadTime(uploadTime);
// 将文件所属项目名称封装到对象中
fileUpload.setProjectName("PPS_OA");
// 获取上传文件服务器磁盘根路径
String distPath = PropertiesSon.getText("server_uploaddir");
// 创建文件工厂
DiskFileItemFactory fac = new DiskFileItemFactory();
// 创建Servlet文件上传对象
ServletFileUpload upload = new ServletFileUpload(fac);
// 封装文件上传编码
upload.setHeaderEncoding("UTF-8");
List fileList = null;
try {
// 获取请求中的所有参数集合
fileList = upload.parseRequest((HttpServletRequest) request);
} catch (FileUploadException ex) {
jm.setError("获取文件失败!");
return jm;
}
String contextPath = "otherFile";
// 遍历参数集合
Iterator<FileItem> it = fileList.iterator();
String savePath = "";
File file = null;
FileItem fileItem = null;
boolean isVideo = false;
while (it.hasNext()) {
FileItem item = it.next();
if (item.isFormField()) {
// 得到上传文件的文件夹名称 将上传用户封装到文件上传对象中
if ("contextPath".equals(item.getFieldName())) {
contextPath = item.getString();
fileUpload.setContextPath(contextPath);
// 拼接文件路径
savePath = distPath + File.separator + contextPath;
File f1 = new File(savePath);
// 验证文件夹是否存在
if (!f1.exists()) {
f1.mkdirs();
}
}
//得到上传用户姓名
if ("empName".equals(item.getFieldName())) {
// 获取用户session中封装的信息对象
String empName = item.getString();
fileUpload.setUserOfUpload(empName);
}
//得到是否上传的是视频
if ("isVideo".equals(item.getFieldName())) {
isVideo = Boolean.valueOf(item.getString());
}
}
}
// 遍历参数集合
Iterator<FileItem> it2 = fileList.iterator();
while (it2.hasNext()) {
FileItem item = it2.next();
// 判断参数是否不是普通form表单项
if (!item.isFormField()) {
// 封装文件上传对象
String name = item.getName();
if (name == null || "".equals(name.trim())) {
continue;
}
fileUpload.setSourceName(name);
//判断文件大小
Long size = item.getSize();
if (size > FILE_MAX_SIZE && !isVideo) {
jm.setError("上传文件不能超过20M!");
return jm;
}
fileUpload.setFileSize(size / FILE_SIZE);
String type = item.getContentType();
fileUpload.setMimeType(type);
// 扩展名格式
if (name.lastIndexOf(".") >= 0) {
String postfix = name.substring(name.lastIndexOf(".") + 1);
String sourceName = name.substring(0, name.lastIndexOf("."));
fileUpload.setSourceName(sourceName);
fileUpload.setPostfix(postfix);
} else {
fileUpload.setSourceName(name);
fileUpload.setPostfix("");
}
do {
// 修改上传文件名称
fileUpload
.setFileEditName(FileUtil.getfileName(
fileUpload.getSourceName(),
fileUpload.getPostfix()).replace("$", "NZ"));
// 获取文件服务器新路径
String newPath = savePath + File.separator
+ fileUpload.getFileEditName();
// 拼接文件访问路径
String url = PropertiesSon.getText("server_accessdir")
+ fileUpload.getContextPath()
+ "/"
+ CommonUtil.getURLEncode(
fileUpload.getFileEditName().substring(0,
fileUpload.getFileEditName().lastIndexOf("_")), "GBK")
+ fileUpload.getFileEditName().substring(
fileUpload.getFileEditName().lastIndexOf("_"));
fileUpload.setUrl(url);
// 根据文件路径创建File对象
file = new File(newPath);
fileItem = item;
} while (file.exists());
//将文件写到目录下
try {
fileItem.write(file);
} catch (Exception e) {
log.error(CommonUtil.getErrorMessage(e));
jm.setError("上传失败!");
return jm;
}
}
}
log.info("上传文件成功!");
commonDao.save(fileUpload);
jm.setIsSuccess(true);
jm.setMessage(CommonUtil.formatObjectToJson(fileUpload).toString());
if (isVideo) {
//如果是视频,异步进行视频格式转换,并截取视频默认预览图片
convertVideoUtil.convertVideo(fileUpload);
}
return jm;
}
/**
* @return the convertVideoUtil
*/
public ConvertVideoUtility getConvertVideoUtil() {
return convertVideoUtil;
}
/**
* @param convertVideoUtil the convertVideoUtil to set
*/
public void setConvertVideoUtil(ConvertVideoUtility convertVideoUtil) {
this.convertVideoUtil = convertVideoUtil;
}
}
- 文件上传下载action 和service
- web service文件上传下载
- java web service 上传下载文件
- rails文件上传下载和删除
- ios和node上传下载文件
- ionic3 文件上传下载和预览
- JSP/servlet实现文件上传下载和删除
- ubuntu SSH 远程登录和上传下载文件
- ftpget 和ftpput命令 tftp 上传下载文件
- Linux scp 远程上传下载文件和文件夹
- ftp文件上传下载和创建多级目录
- 文件上传下载
- 文件上传下载
- 上传下载文件
- 文件上传下载
- 文件上传下载
- 文件上传下载
- 文件上传下载
- 将C++代码转换为HTML
- BASH 数组用法小结 及 循环用法
- 各种排序方法的对比。
- U-Boot启动过程
- ios: webview 中 加载状态显示两种方法
- 文件上传下载action 和service
- 数据泵expdp和impdp用法
- 错误1项目文件必须在引用列表中包含 .NET Framework 程序集“WindowsBase, PresentationCore, PresentationFramework”。C:\Win
- OpenCV使用Kinect和其他OpenNI兼容的深度传感器(opencv_user.pdf 3.1章)
- AJAX $.toJSON的用法或把数组转换成json类型
- 3D游戏编程入门(十一)D3D基础之FVF格式,图元
- NTSC PAL SECAM
- QT信号和槽以结构体为参数传递复杂数据
- 3D游戏编程入门(十)D3D程序Debug技巧和D3D基础