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>