Java 一个文件上传工具类
最编程
2024-06-23 11:50:29
...
/**
* 文件上传
*
* @author cary
* @since 2012-12-19 下午2:22:12
*/
public class FileUploader {
static final String UPLOAD_PATH_KEY = "upload.dir";
private static Logger logger = Logger.getLogger(FileUploader.class);
/**
* 定义允许上传的文件扩展名
*/
private Map<String, String> extMap = new HashMap<String, String>();
/**
* 文件上传目录
*/
private String savePath;
/**
* 文件完整目录
*/
private String saveUrl;
/**
* 空构造器
*/
public FileUploader() {
// 允许上传的文件类型
extMap.put("extName", "gif,jpg,jpeg,png,bmp,xls,xlsx");
}
/**
* @param request 请求对象
* @param upload 被上传的文件
* @param uploadFileName 被上传的文件名
* @param dirName 文件类型对应的路径
* @param maxSize 文件大小阈值
* @return
* @throws IOException
*/
public String[] uploadFile(HttpServletRequest request, File upload, String uploadFileName, String dirName,
Long maxSize) throws IOException {
/**
* 保存上传文件返回信息
*/
String[] infos = new String[2];
/**
* 获取文件上传目录
*/
String extName = uploadFileName.substring(uploadFileName.lastIndexOf(".") + 1, uploadFileName.length());
infos[0] = this.validateFields(request, extName, dirName, maxSize);
String prefixName = uploadFileName.substring(0, uploadFileName.lastIndexOf("."));
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhh24mmss");
String ymd = sdf.format(new Date());
String realName = prefixName + ymd + "." + extName;
/**
* 获取文件全路径
*/
saveUrl = savePath + realName;
if (infos[0].equals("true")) {
saveFile(upload, savePath, realName);
infos[1] = saveUrl;
}
return infos;
}
/**
* 上传验证,并初始化文件目录
*
* @param request 请求对象
* @param extName 扩展名
* @param dirName 子目录名
* @param maxSize 文件大小阈值
* @throws IOException
*/
private String validateFields(HttpServletRequest request, String extName, String dirName, Long maxSize)
throws IOException {
String errorInfo = "true";
// 获取内容类型
String contentType = request.getContentType();
String extType = extMap.get("extName");
int contentLength = request.getContentLength();
String path = ConfigUtil.getString(FileUploader.UPLOAD_PATH_KEY);
savePath = path + "/" + dirName + "/";
File uploadDir = new File(savePath);
if (contentType == null || !contentType.startsWith("multipart")) {
logger.error("请求不包含multipart/form-data流");
return "请求不包含multipart/form-data流";
}
if (extType.indexOf(extName) == -1) {
return "不允许上传的文件类型[" + "." + extName + "]";
}
if (maxSize < contentLength) {
logger.error("上传文件大小超出文件最大大小");
return "上传文件大小超出文件最大大小[" + maxSize + "]";
}
if (!ServletFileUpload.isMultipartContent(request)) {
return errorInfo = "请选择文件";
}
if (!uploadDir.exists()) {// 检查目录
logger.warn("上传目录[" + savePath + "]不存在");
uploadDir.mkdir();
}
if (!uploadDir.canWrite()) {
return "上传目录[" + savePath + "]没有写权限";
}
return errorInfo;
}
/**
* 保存文件
*
* @param upload 待上传文件
* @param savePath 文件存储目录
* @param realName 原始文件名
* @throws IOException
*/
public void saveFile(File upload, String savePath, String realName) throws IOException {
File target = new File(savePath, realName);
FileUtils.copyFile(upload, target);
}
public static void main(String[] args) {
File dirFile = new File("/usr/local/upload/excel");
if (!dirFile.exists()) {
dirFile.mkdirs();
}
}
2.action调用代码
FileUploader fileUploader = new FileUploader();
2.action调用代码
FileUploader fileUploader = new FileUploader();
上一篇: JAVA 实现文件上传和下载
下一篇: Java 向服务器上传文件图像方法
推荐阅读
-
Java 8新特性探究(十三)JavaFX 8新特性以及开发2048游戏-JavaFX历史## 跟java在服务器端和web端成绩相比,桌面一直是java的软肋,于是Sun公司在2008年推出JavaFX,弥补桌面软件的缺陷,请看下图JavaFX一路走过来的改进 从上图看出,一开始推出时候,开发者需使用一种名为JavaFX Script的静态的、声明式的编程语言来开发JavaFX应用程序。因为JavaFX Script将会被编译为Java bytecode,程序员可以使用Java代码代替。 JavaFX 2.0之后的版本摒弃了JavaFX Script语言,而作为一个Java API来使用。因此使用JavaFX平台实现的应用程序将直接通过标准Java代码来实现。 JavaFX 2.0 包含非常丰富的 UI 控件、图形和多媒体特性用于简化可视化应用的开发,WebView可直接在应用中嵌入网页;另外 2.0 版本允许使用 FXML 进行 UI 定义,这是一个脚本化基于 XML 的标识语言。 从JDK 7u6开始,JavaFx就与JDK捆绑在一起了,JavaFX团队称,下一个版本将是8.0,目前所有的工作都已经围绕8.0库进行。这是因为JavaFX将捆绑在Java 8中,因此该团队决定跳过几个版本号,迎头赶上Java 8。 ##JavaFx8的新特性 ## ###全新现代主题:Modena 新的Modena主题来替换原来的Caspian主题。不过在Application的start方法中,可以通过setUserAgentStylesheet(STYLESHEET_CASPIAN)来继续使用Caspian主题。 参考http://fxexperience.com/2013/03/modena-theme-update/ ###JavaFX 3D 在JavaFX8中提供了3D图像处理API,包括Shape3D (Box, Cylinder, MeshView, Sphere子类),SubScene, Material, PickResult, LightBase (AmbientLight 和PointLight子类),SceneAntialiasing等。Camera类也得到了更新。从JavaDoc中可以找到更多信息。 ###富文本 强化了富文本的支持 ###TreeTableView ###日期控件DatePicker 增加日期控件 ###用于 CSS 结构的公共 API
-
Java表情转译工具类适用于Mac系统
-
深入探究Lustre分布式文件系统中的Utils工具: 一个详尽解析
-
一个用于转换C代码到Java的在线工具
-
推荐六个实用工具类应用,最后一个备受期待!
-
如何在Java中创建测试类? - 编写一个Java类的方法
-
Intellij IDEA 插件开发入门详解 - 如何添加 Application 和 Project Component,并创建 Action? 在本文中,我们将详细介绍如何在 IntelliJ IDEA 中添加 Application 和 Project Component,并且通过这些组件来创建一个简单的 Action。 首先,我们将在 src 目录上使用 Alt+Insert 快捷键打开 New 对话框,然后从中选择 Application Component 并输入名称如 MyComponent。接下来,我们在 MyComponent 类中添加一个 sayHello 方法并编写相关逻辑。 然后,我们需要为我们的插件添加一个 Action,使用户可以通过菜单或其它方式访问它。为此,我们将创建一个新的类 SayHelloAction 继承自 AnAction 类,并在 actionPerformed 方法中获取 Application 和 MyComponent 对象,最后调用 MyComponent 的 sayHello 方法。 最后,我们需要为我们的插件配置相关的文件以确保它可以正常运行。在本文中,我们将详细介绍如何进行这些配置。
-
了解assimp库:一个能转换三维模型文件的实用工具
-
Java实现视频分割和合并:将大文件拆分成多个小文件并将其重新组合为一个大文件
-
将多个Java视频文件合成为一个新视频的技巧