
有一种叫jspsmartupload的包用来简化文件上传下载的编写里面可以获取文件大年夜小//取得文件 comjspsmartuploadFile myFile = mySmartUploadgetFiles()getFile(0);//取得文件名 String fileName = myFilegetFileName();//取得文件大年夜小 int fileSize = myFilegetSize();
在webconfig在限制上传文件的大小
不过首先我要告诉你。这样子达不到你的要求的。比如你想要限到30M但你会发现。上传20M时就出错了。因为IIS那里就会认为你进行DOS攻击了。把你的页面给喀嚓了。这样子页面就会有问题。所以最好的方法是在客户端用JS进行验证。
不过你要webconfig的做法我就给你一个好了
maxRequestLength的单位是K
<xml version="10" encoding="utf-8" >
<configuration>
<systemweb>
<>
1浏览上传:
<form action="subasp" method="post" enctype="multipart/form-data" target="_self">
<input type="file" name="src" size="20" value="浏览">
<input type="submit" value="上传" name="B1" IsShowProcessBar="True">
</form>
subasp 执行上传并返回地址,同时保存文件名在session(ccc)中
<!--#include FILE="uploadinc"-->
<%
dim upload,file,formName,formPath,iCount,fileformat
set upload=new upload_F
function MakedownName()
dim fname
fname = now()
fname = replace(fname,"-","")
fname = replace(fname," ","")
fname = replace(fname,":","")
fname = replace(fname,"PM","")
fname = replace(fname,"AM","")
fname = replace(fname,"上午","")
fname = replace(fname,"下午","")
fname = int(fname) + int((10-1+1)Rnd + 1)
MakedownName=fname
end function
formPath="/upload/"
iCount=0
for each formName in uploadfile ''列出所有上传了的文件
set file=uploadfile(formName) ''生成一个文件对象
fileformat=lcase(right(filefilename,4))
if fileformat="asp" or fileformat="htm" then
responsewrite"<script>alert('文件格式不对,请重新上传!');location='"&requestServerVariables(">
在Webconfig文件里可以设置文件上传的大小
使用方法如下:
<>
FileInputStream ins = new FileInputStream(file);
if (insavailable() > 1024 1024 3) {
filedelete();
data = "{success:false,info:'上传的文件不能超过3M,请重新上传'}";
superoutString(data);
return null;
}
public static void getFileSize(String path){
//传入文件路径
File file = new File(path);
//测试此文件是否存在
if(fileexists()){
//如果是文件夹
//这里只检测了文件夹中第一层 如果有需要 可以继续递归检测
if(fileisDirectory()){
int size = 0;
for(File zf : filelistFiles()){
if(zfisDirectory()) continue;
size += zflength();
}
Systemoutprintln("文件夹 "+filegetName()+" Size: "+(size/1024f)+"kb");
}else{
Systemoutprintln(filegetName()+" Size: "+(filelength()/1024f)+"kb");
}
//如果文件不存在
}else{
Systemoutprintln("此文件不存在");
}
}
以上就是关于javaweb如何获得文件上传大小全部的内容,包括:javaweb如何获得文件上传大小、如何限制上传文件的大小、asp在上传文件时如何获取待传文件的大小等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)