个人空间大小的限制 JSF异常

 1.个人空间大小的限制
知识点:FacesContext?
upload.jsp
<x:inputFileUpload id="fileId" value="#{FileBean.myfile}" storage="file"  />上传
FileBean.java
(对每个用户创建一个文件夹PP,设定个人空间大小20M)
// 上传已选择的文件
 public String upLoad() {
  if (myfile != null) {
   this.fileName = new File(myfile.getName()).getName();
   File docDir = new File(FacesUtils.getServletContext().getRealPath(
     "/PP"));
   if (!docDir.exists()) {
    docDir.mkdir();
   }
   File dir = new File(FacesUtils.getServletContext()
     .getRealPath(
       "/PP/"
         + (String) FacesUtils
           .getSessionAttribute("userId")));
   if (dir.isDirectory() && !dir.exists()) {
    dir.mkdir();
   }

   /*超过了上传的大小,就抛出异常*/
   String userId=(String) FacesUtils.getSessionAttribute("userId");
            Long upSize=getSize(docDir+"//"+userId);//每个用户规定空间已用大小
   if(upSize>20*1024)    //现规定固定空间大小为20M
            {
             throw new FileException(FileException.EXCEED_SIZE,new String[]{});
            }
   /*结束*/
   UploaderHelper.upload(myfile, "PP//"+ userId);
  }
  return "upload:ok";
 }
 
 //pp 获取每个用户规定空间已上传大小
 public Long getSize(String strName){
     Long TotalSize=0L;
     File f=new File(strName);
//     if(f.isFile())
//      return f.length();
//     else{
      if(f.isDirectory()){
       File[] contents=f.listFiles();
       for(int i=0;i<contents.length;i++){
        if(contents[i].isFile())
         TotalSize+=contents[i].length();
        else{
         if(contents[i].isDirectory())
          TotalSize+=getSize(contents[i].getPath());
        }
       }
      }
    Long ss=myfile.getSize(); //新上传文件的大小,单位字节
     TotalSize+=ss;
     TotalSize=TotalSize/1024;//单位KB
//     }
     return TotalSize;
  }

UploaderHelper
package com.jsfabc.jsh.utils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import org.apache.myfaces.custom.fileupload.UploadedFile;

public class UploaderHelper {
 public static boolean upload(UploadedFile uploadfile, String folder) {
  String fileName = new File(uploadfile.getName()).getName();
  InputStream in;
  try {
   in = uploadfile.getInputStream();
   FacesContext ctx = FacesContext.getCurrentInstance();
   HttpSession hs = (HttpSession) ctx.getExternalContext().getSession(
     true);
   String directory = hs.getServletContext().getRealPath("//")
     + folder;
   File fileDir = new File(directory);
   if (!fileDir.exists()) {
    if (!fileDir.mkdir()) {
    }
   }
   String file = hs.getServletContext().getRealPath("//") + folder
     + "//" + fileName;
   BufferedOutputStream out = new BufferedOutputStream(
     new FileOutputStream(file));
   byte[] buffer = new byte[1024 * 2];
   int length;
   while ((length = in.read(buffer, 0, buffer.length)) > 0) {
    out.write(buffer, 0, length);
   }
   out.flush();
   out.close();
   return true;
  } catch (IOException e) {
   e.printStackTrace();
   return false;
  }

 }
}

 

 


2.JSF超过大小的异常
messages.properties
exceed.size=超过了空间的上传大小
BusinessException.java
public class BusinessException extends RuntimeException {
 private String key;

 private String[] params;

 public String getKey() {
  return key;
 }

 public void setKey(String key) {
  this.key = key;
 }

 public String[] getParams() {
  return params;
 }

 public void setParams(String[] params) {
  this.params = params;
 }

 public BusinessException(String msg, String[] params) {
  super(msg);
  this.key = msg;
  this.params = params;
 }

}
FileException.java
import com.whlongyi.common.condition.BusinessException;
public class PersonalFileException extends BusinessException {
 public PersonalFileException(String msg, String[] params) {
  super(msg, params);
 }
 public static final String EXCEED_SIZE="exceed.size";

}

抛出
if(upSize>2*1024)    //现规定固定空间大小为20M
            {
             throw new FileException(FileException.EXCEED_SIZE,new String[]{});
            }

显示
<h:messages></h:messages>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值