FileUpBean.java
上传用户:u_thks
上传日期:2022-07-31
资源大小:1910k
文件大小:9k
- package com.gamvan.fileUpload;
- import java.io.File;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.StringTokenizer;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import javax.servlet.ServletConfig;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.jsp.PageContext;
- import com.gamvan.tools.TypeChange;
- public class FileUpBean {
- public String message = "";
- public String fileSavePath = ""; // 文件上传后保存的路径。
- public String fileTempPath = ""; //文件上 传后保存的路径。
- public String vPath = ""; //文件上传的相对路径
- private String newFileName = "";
- private String oldFileName = "";
- private String oldFilePath = "";
- private String fileExt = ""; //文件扩展名
- private int maxFileSize = 0; //文件最大值。单位KB
- public long fileSize = 0; //文件大小,单位字节
- public long myFileSize = 0; //个人上传空间限制。结合club。
- private long readsBytes = 0; //读取上传进度
- public String allowedType = ""; //允许上传得文件类型。
- protected HttpServletRequest g_request;
- protected HttpServletResponse g_response;
- protected ServletContext g_application;
-
- public final void init(ServletConfig servletconfig) throws ServletException
- {
- g_application = servletconfig.getServletContext();
- }
- public void service(HttpServletRequest httpservletrequest,HttpServletResponse httpservletresponse) throws ServletException,IOException
- {
- g_request = httpservletrequest;
- g_response = httpservletresponse;
- }
- public final void initialize(ServletConfig servletconfig,HttpServletRequest httpservletrequest,HttpServletResponse httpservletresponse) throws ServletException
- {
- g_application = servletconfig.getServletContext();
- g_request = httpservletrequest;
- g_response = httpservletresponse;
- }
- public final void initialize(PageContext pagecontext) throws ServletException
- {
- g_application = pagecontext.getServletContext();
- g_request = (HttpServletRequest)pagecontext.getRequest();
- g_response = (HttpServletResponse)pagecontext.getResponse();
- }
-
- HashMap hm = new HashMap();
-
- public boolean saveFile() throws Exception {
- boolean bea =false;
- try {
- //为该请求创建一个句柄,通过它来解析请求。执行
- //解析后,所有的表单项目都保存在一个List中。
- DiskFileUpload dfu = new DiskFileUpload();
- /*开始读取上传信息
- * 以参数形式传递文件上传大小限制,临时目录,最大内存使用数
- * List fileItems = dfu.parseRequest(g_request,4096, maxFileSize,fileTempPath);
- */
-
- // 设置允许用户上传文件大小,单位:字节,这里设为2m
- dfu.setSizeMax(maxFileSize);
- // 设置最多只允许在内存中存储的数据,单位:字节
- dfu.setSizeThreshold(4096);
- // 设置一旦文件大小超过setSizeThreshold()的值时数据存放在硬盘的目录
- dfu.setRepositoryPath(fileTempPath);
-
-
- // parseRequest是DiskFileUpload继承自FileUpoadBase的方法
- //通过句柄解析请求,解析得到的项目保存在一个List中
- List fileItems = dfu.parseRequest(g_request);
-
-
- /*
- * 通过循环依次获得List里面的文件项目。要区分表示
- * 文件的项目和普通的表单输入项目,使用isFormField()方法。
- * 根据处理请求的要求,我们可以保存上载的文件,
- * 或者一个字节一个字节地处理文件内容,或者打开文件的输入流。
- */
- Iterator iter = fileItems.iterator();
-
- //正则匹配,过滤路径取文件名
- String separator = java.io.File.separator;
- String regExp=".+"+separator+separator+"(.+)$";
- Pattern p = Pattern.compile(regExp);
- while (iter.hasNext()) {
- /**
- * 进度读取
- */
- this.readsBytes = dfu.getReadsBytes();
- FileItem item = (FileItem)iter.next();
- //忽略其他不是文件域的所有表单信息
- if (item.isFormField()) {
- hm.put(item.getFieldName(),item.getString("UTF-8"));
- }else{
- String name = item.getName();
- oldFilePath = name;
- fileSize = item.getSize();
- if(myFileSize<fileSize/1024){
- message = "<font color="#bb0000"> 您的空间已满、或者你选择的文件过大,不能执行上载。</font>";
- message += "<br/>您的上传的文件大小约为:" + fileSize/1024;
- message += "<br/>您的剩余上传空间约为:" + myFileSize;
- return false;
- }
- if((name==null||name.equals("")) && fileSize==0)
- continue;
- Matcher m = p.matcher(name);
- boolean result = m.find();
- oldFileName = m.group(1); //取得原文件名
- int f = 0;
- f = oldFileName.lastIndexOf('.') + 1;
- fileExt = oldFileName.substring(f, oldFileName.length()).toLowerCase(); //取得文件扩展名
- newFileName = newFileName + "." + fileExt;
- if (result){
- StringTokenizer st = new StringTokenizer(allowedType, "|");
- String[] ishave = new String[st.countTokens()];
- boolean passType = false; //判断文件类型是否属于允许的上传类型
- for(int i=0;st.hasMoreTokens();i++){
- ishave[i]=st.nextToken().trim();
- oldFileName = oldFileName.toLowerCase(); //全部替换成小写
- if (oldFileName.endsWith(ishave[i])){
- passType = true;
- }
- }
- if(!passType){
- message = "<a href="javascript:history.back()">"+oldFileName+":此文件类型被禁止上传。</a>";
- return false;
- }else{
- try{
- //保存上传的文件到指定的目录
- item.write(new File(fileSavePath + newFileName));
- message = " "+fileSize+"<br>";
- bea = true;
- }
- catch(Exception e){
- message = e.toString();
- return false;
- }
- }
- }else{
- message = "fail to upload" ; //上传失败!
- bea = false;
- }
- }
- }
- }catch (Exception e){
- message = e.toString();
- }
- return bea;
- }
-
- /**
- * 取表单数据方法
- */
- public String getFiledNameValue(String id){
- String str = new String();
- if(hm.containsKey(id)){
- str = hm.get(id).toString();
- }else{
- str = null;
- }
- return str;
- }
-
- public String getParameter(String s){
- return s;
- }
-
- /**
- * 进度读取
- */
- public long getReadsBytes(){
- return this.readsBytes;
- }
-
-
- public void setFileSavePath(String s){
- this.fileSavePath = s;
- }
-
-
- public void setFileTempPath(String s){
- this.fileTempPath = s;
- }
-
- public void setMyFileSize(double d){
- this.myFileSize = TypeChange.doubleToLong(d);
- }
-
- public void setMyFileSize(long i){
- this.myFileSize = i;
- }
-
- public void setMaxFileSize(int i){
- this.maxFileSize = i;
- }
-
- public void setAllowedType(String s){
- this.allowedType = s;
- }
-
- public void setNewFileName(String s){
- this.newFileName = s;
- }
-
- public String getFileExt(){
- return this.fileExt;
- }
-
- public String getNewFileName(){
- return this.newFileName;
- }
-
-
- public String getOldFileName(){
- return this.oldFileName;
- }
-
-
- public String getOldFilePath(){
- return this.oldFilePath;
- }
-
- public String getFileSavePath(){
- return this.fileSavePath;
- }
-
- public long getMyFileSize(){
- return this.myFileSize;
- }
-
- public long getFileSize(){
- return this.fileSize;
- }
-
- public String getMessage(){
- return this.message;
- }
-
- }