<body>
<h2>上传图书课件</h2>
<form action="LimitFile" name="one" enctype="multipart/form-data" method="post">
选择一个rar文件:
<input type="file" name="fileupload" value="upload" />
<input type="submit" value="上传"> <input type="reset" value="取消">
</form>
</body>
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String uploadpath = "";
DiskFileItemFactory factory = new DiskFileItemFactory();
//设置是否使用临时文件保存解析出来的数据的那个临界值,该方法传入参数的单位是字节。
factory.setSizeThreshold(30 * 1024);
//用于设置setSizeThreshold()方法中提到的临时文件的存放目录,这里要求使用绝对路径。
factory.setRepository(factory.getRepository());
ServletFileUpload upload = new ServletFileUpload(factory);
List list = null;
try{
list = upload.parseRequest(request);
String[] limit = new String[]{".jpg", ".gif", ".png", ".bmp"};
//定义限制的文件类型
SuffixFileFilter filter = new SuffixFileFilter(limit);
//获取SuffixFileFilter实例
Iterator iterator = list.iterator();
while(iterator.hasNext()){
FileItem item =(FileItem)iterator.next();
if(!item.isFormField()){
String filePath = item.getName();
if(filePath != null){
File filename= new File(filePath);
File uploadFile = new File(request.getSession().getServletContext().getRealPath("/") + "upload");
uploadpath = uploadFile.getAbsolutePath()+File.pathSeparator + uploadpath;
//因为路径后面多了个";"号,所以要去掉
uploadpath = uploadpath.substring(0, uploadpath.length()-1);
File saveFile = new File(uploadpath,filename.getName());
boolean flag = filter.accept(saveFile);
if(flag){
out.print("禁止上传传图片文件");
break;
}else{
try {
item.write(saveFile);
out.print("文件上传成功");
} catch (Exception e) {
out.print("文件上传失败了");
e.printStackTrace();
}
}
}
}
}
}catch(FileUploadException e){
e.printStackTrace();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有