jsp文件上传如何规定大小
答案:3 悬赏:50 手机版
解决时间 2021-04-02 10:30
- 提问者网友:送舟行
- 2021-04-01 23:18
jsp文件上传如何规定大小
最佳答案
- 五星知识达人网友:狂恋
- 2021-04-02 00:13
以下是代码:
String fileName = " ";
String appPath = request.getSession().getServletContext().getRealPath("/") ;
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(cacheSize);//缓冲大小
File temFile = new File(appPath+tempFileFold);//超过缓冲小放在临时文件夹,再一步一步上传
if(!temFile.exists()){
temFile.mkdirs();
}
factory.setRepository(temFile);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(maxFileSize);//最大大小
List fileList = null ;
try {
fileList = upload.parseRequest(request);
} catch (FileUploadException e) {
if (e instanceof SizeLimitExceededException) {
System.out.println("超过大小了,返回!");
double maxSize =maxFileSize/(1024.0*1024.0);
if(maxSize>1.0){
float fileSize = Math.round(maxSize*1000)/1000;
request.setAttribute("message", MessageResource.readByString("file_size_overflow")+fileSize+"M");
}else{
double kMaxSize = maxFileSize/(1024.0);
float fileSize = Math.round(kMaxSize*100)/100;
request.setAttribute("message", MessageResource.readByString("file_size_overflow")+fileSize+"K");
}
request.setAttribute("page", request.getParameter("failpage"));
System.out.println("page:"+request.getAttribute("page")+" messgae:"+request.getAttribute("message"));
return "";
}
e.printStackTrace();
}
if (fileList == null || fileList.size() == 0) {
System.out.println("空文件,返回!");
return "";
}
// 得到所有上传的文件
Iterator fileItr = fileList.iterator();
// 循环处理所有文件
while (fileItr.hasNext()) {
FileItem fileItem = null;
String path = null;
long size = 0;
// 得到当前文件
fileItem = (FileItem) fileItr.next();
// 忽略简单form字段而不是上传域的文件域(等)
if (fileItem == null || fileItem.isFormField()) {
continue;
}
// 得到文件的完整路径
path = fileItem.getName();
// 得到文件的大小
size = fileItem.getSize();
if ("".equals(path) || size == 0) {
System.out.println("空文件2,返回!");
return "" ;
}
String fileName = " ";
String appPath = request.getSession().getServletContext().getRealPath("/") ;
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(cacheSize);//缓冲大小
File temFile = new File(appPath+tempFileFold);//超过缓冲小放在临时文件夹,再一步一步上传
if(!temFile.exists()){
temFile.mkdirs();
}
factory.setRepository(temFile);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(maxFileSize);//最大大小
List fileList = null ;
try {
fileList = upload.parseRequest(request);
} catch (FileUploadException e) {
if (e instanceof SizeLimitExceededException) {
System.out.println("超过大小了,返回!");
double maxSize =maxFileSize/(1024.0*1024.0);
if(maxSize>1.0){
float fileSize = Math.round(maxSize*1000)/1000;
request.setAttribute("message", MessageResource.readByString("file_size_overflow")+fileSize+"M");
}else{
double kMaxSize = maxFileSize/(1024.0);
float fileSize = Math.round(kMaxSize*100)/100;
request.setAttribute("message", MessageResource.readByString("file_size_overflow")+fileSize+"K");
}
request.setAttribute("page", request.getParameter("failpage"));
System.out.println("page:"+request.getAttribute("page")+" messgae:"+request.getAttribute("message"));
return "";
}
e.printStackTrace();
}
if (fileList == null || fileList.size() == 0) {
System.out.println("空文件,返回!");
return "";
}
// 得到所有上传的文件
Iterator fileItr = fileList.iterator();
// 循环处理所有文件
while (fileItr.hasNext()) {
FileItem fileItem = null;
String path = null;
long size = 0;
// 得到当前文件
fileItem = (FileItem) fileItr.next();
// 忽略简单form字段而不是上传域的文件域(等)
if (fileItem == null || fileItem.isFormField()) {
continue;
}
// 得到文件的完整路径
path = fileItem.getName();
// 得到文件的大小
size = fileItem.getSize();
if ("".equals(path) || size == 0) {
System.out.println("空文件2,返回!");
return "" ;
}
全部回答
- 1楼网友:酒者煙囻
- 2021-04-02 02:16
这个要看你是使用什么开源上传控件了。一般的在action获取上传的对象,然后对象一般都获得当前上传大小的方法然后进行判断 然后进行控制是否允许继续上传,不允许返回页面提示即可
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯