海口seo网站推广给别人做网站去掉版权
web/
2025/9/27 6:13:17/
文章来源:
海口seo网站推广,给别人做网站去掉版权,用狐狸做logo的网站,上蔡县做彩票网站网上找了一些#xff0c;都是基本介绍#xff0c;没有直接就可以使用类#xff0c;在实际工作当中都有适合当前项目的上传文件的方法#xff0c;本人写了一个类#xff0c;比较基础#xff0c;但呆以满足项目的上传文件功能#xff0c;使用了commons-fileupload这个组件…网上找了一些都是基本介绍没有直接就可以使用类在实际工作当中都有适合当前项目的上传文件的方法本人写了一个类比较基础但呆以满足项目的上传文件功能使用了commons-fileupload这个组件代码如下首先要在maven中加入依赖commons-fileuploadcommons-fileupload1.3.3UploadUtil.javapackage cn.form1.utils;import org.springframework.util.ClassUtils;import org.springframework.web.multipart.MultipartFile;import org.springframework.web.multipart.MultipartHttpServletRequest;import org.springframework.web.multipart.commons.CommonsMultipartResolver;import javax.servlet.http.HttpServletRequest;import java.io.File;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.*;/*** 文件上传类*/public class UploadUtil {//上传的文件大小限制 (0-不做限制) 单位字节private long maxSize 0;//允许上传的文件后缀如.jpg|.png|.git|.jpeg为空不做限制private String exts;//保存根路径会在tomcat的webapps自动创建该文件夹private String rootPath uploadFile/;//保存路径如 userimageprivate String savePath ;//子目录创建方式默认年-月private String subName yyyy-MM;//是否启动时间格式的子目录private boolean isSubName true;//上传的文件名称private List fileNames;//上传错误信息private String error;public UploadUtil() {this.fileNames new ArrayList();}public long getMaxSize() {return maxSize;}public void setMaxSize(long maxSize) {this.maxSize maxSize;}public String getExts() {return exts;}public void setExts(String exts) {this.exts exts;}public String getRootPath() {return rootPath;}public void setRootPath(String rootPath) {this.rootPath rootPath;}public String getSavePath() {return savePath;}public void setSavePath(String savePath) {this.savePath savePath;}public String getSubName() {return subName;}public void setSubName(String subName) {this.subName subName;}public boolean getIsSubName() {return isSubName;}public void setIsSubName(boolean isSubName) {this.isSubName isSubName;}public String getError() {return error;}public void setError(String error) {this.error error;}public List getFileNames() {return fileNames;}public void setFileNames(List fileNames) {this.fileNames fileNames;}/*** 上传文件* param request 当前请求的request*/public boolean upload(HttpServletRequest request) throws IllegalStateException, IOException {//创建一个通用的多部分解析器CommonsMultipartResolver multipartResolver new CommonsMultipartResolver(request.getSession().getServletContext());//判断 request 是否有文件上传,即多部分请求,其实判断是否为(enctypemultipart/form-data methodPOST)if(multipartResolver.isMultipart(request)){//转换成多部分requestMultipartHttpServletRequest multiRequest (MultipartHttpServletRequest)request;//取得request中的所有文件名Iterator iter multiRequest.getFileNames();//记数器int num 0;//web服务器文件根路径String webFilePath;//为批量上传所以如果有下一个信息能循环输出while(iter.hasNext()){webFilePath ;//取得上传文件MultipartFile file multiRequest.getFile(iter.next());//如果typefile中有文件上传if(!file.isEmpty()){//取得当前上传文件的文件名称String fileName file.getOriginalFilename();//获取文件大小单位字节long fileSize file.getSize();/* 检查文件大小 */if (!this.checkSize(fileSize)) {this.setError(上传文件大小不符);return false;}//获取文件的后缀名String suffixName fileName.substring(fileName.lastIndexOf(.));/* 判断文件后缀名是否合法 */if(!this.checkExt(suffixName)){this.setError(上传文件后缀不允许);return false;}//使用GUID重命名图片名称fileName UUID.randomUUID() suffixName;/* 获取Tomcat的webapps根目录 */String projectPath this.tomcatPath();/* 是否生成子目录 */String dateDir this.dateDir();//web服务器根目录文件路径webFilePath this.getRootPath() this.getSavePath() dateDir fileName;//文件最终保存全路径String fileNamePath projectPath webFilePath;//创建File对象File localFile new File(fileNamePath);//检测是否存在目录不存在则创建if (!localFile.getParentFile().exists()) {localFile.getParentFile().mkdirs();}//执行上传文件file.transferTo(localFile);}//累加保存生成文件名this.fileNames.add(num,webFilePath);num;}}//没有上传任何文件返回true去判断返回文件List中的值return true;}/*** 检查文件大小是否合法* param size 文件大小单位/字节* return boolean*/private boolean checkSize(long size) {return !(size this.getMaxSize()) || (0 this.getMaxSize());}/*** 检查上传的文件后缀是否合法* param ext 后缀* return boolean*/private boolean checkExt(String ext){if(this.getExts().isEmpty()){return true;}else{if(this.getExts().indexOf(ext) ! -1){return true;}}return false;}/*** 返回Tomcat的webapps根目录* return String 路径*/private String tomcatPath(){//获取当前项目的运行环境根目录,如/C:/myJavaEEWorkSpace/SpringGirl/target/classes/String projectPath ClassUtils.getDefaultClassLoader().getResource().getPath();//返回Tomcat的webapps根目录 (考虑到每次发布会覆盖war文件最好存在war外面)projectPath projectPath ../../../;return projectPath;}/*** 是否生成子目录返回子目录名称* return String 目录名称*/private String dateDir(){//是否生成子目录String dateDir;if(this.getIsSubName()) {//设置文件存放子目录SimpleDateFormat df new SimpleDateFormat(this.getSubName());// 设置日期格式dateDir df.format(new Date());// new Date()为获取当前系统时间dateDir dateDir /;}else{dateDir ;}return dateDir;}}使用方法/** 测试上文件传类* */RequestMapping(value /uploadclass)ResponseBodypublic String uploadclass(HttpServletRequest request) throws IllegalStateException, IOException{UploadUtil upload new UploadUtil();upload.setMaxSize(50000);upload.setExts(.jpg|.png|.gif|.jpeg);upload.setSavePath(mydogimg/);if(upload.upload(request)){List list upload.getFileNames();String str (String) list.get(0);return str;}else{return upload.getError();}}支持单个或批量上传如果typefile没有选择文件就会返回空的字符串上传文件是否为必须的需要用js去判断
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/80999.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!