文件上传中的安全问题

很多网站都提供了文件上传功能,但若没有充分考虑其安全问题,往往会被恶意用户对网站进行破坏。

可以使用jspsmartUpload.jar实现文件上传功能。

SmartUpload类用于实现文件的上传和下载操作。

首先SmartUpload对象调用initialize(pageContext)

设置最大空间setMaxFileSize(2*1024*1024)

设置限制的文件 setDeniedFilesList("exe,jsp,bat");

设置只允许上传的扩展名setAllowedFilesList("txt,doc,,") 若表示允许没有扩展名的文件上传,以“,,”表示。这句话表示只允许上传*.txt *.doc和不带扩展名的文件

执行上传的方法 upload();

getFiles();该方法会获取全部上传文件,以Files对象形式返回。


com.jspsmart.upload.File类  此类不同于java.io.File,此类用户保存上传的文件的相关信息。

com.jspsmart.upload.Files类  该类储存了所有上传的文件,通过类中的方法可获得上传文件的数量和总长度等信息。


doup.jsp

<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="com.jspsmart.upload.File" %>
<%@ page import="com.jspsmart.upload.Files" %>
<jsp:useBean id="myup" class="com.jspsmart.upload.SmartUpload"/>
<center>正在上传文件,请稍等……</center>
<%
  String filedir="/file/";//上传目标文件夹
  String errors="";
  long maxsize=2*1024*1024;//上传文件的最大空间限制 
  boolean allow=true;
  
  try{
	  myup.initialize(pageContext);//初始化 
	  myup.setMaxFileSize(maxsize);//设置文件的最大空间限制 
	  myup.setDeniedFilesList("exe,jsp,bat");//设置限制的文件 
	  myup.upload();//执行上传方法 
  }catch(SecurityException e){
	  allow=false;
	  errors+="禁止上传\"*.exe\"、\"*.jsp\"、\".bat\"文件";//上传失败提示信息 
	  e.printStackTrace();
  }

  if(allow){//如果上传成功 
      try{
	      Files files=myup.getFiles();//获取上传的文件对象 
      	  for(int i=0;i<files.getCount();i++){//遍历文件 
      	      File singlefile=files.getFile(i);
      	      if(!singlefile.isMissing()){	    	  
      	          String name=singlefile.getFileName();//获取上传文件的文件名 	    	  
      	          singlefile.saveAs(filedir+name,File.SAVEAS_VIRTUAL);
      	          errors+="<li>文件"+(i+1)+"上传成功!</li>";//上传成功的信息         				  
      	      }		  
      	  }	  
      }catch(java.lang.NumberFormatException e){
	      errors="文件上传失败!";
    	  e.printStackTrace();
      }
  }
  request.setAttribute("errors",errors);//保存提示信息 
%>
<jsp:forward page="fileup.jsp"/>
fileup.jsp

<%@ page contentType="text/html;charset=gb2312"%>
<%
   String errors=(String)request.getAttribute("errors");
   if(errors==null||errors.equals(""))
	   errors="<li>请选择要上传的文件!</li>";
%>
<html>
  <head>
    <title>文件上传</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
  </head>  
  <body>
    
                 <form action="doup.jsp" method="POST" enctype="multipart/form-data">
                 <table border="1" height="200" width="450" bordercolor="gray" bordercolorlight="gray" bordercolordark="white" cellspacing="0" rules="none">
                     <tr bgcolor="#B3DC38" height="35"><td align="center" colspan="3"><%=errors%></td></tr>
                     <% for(int i=1;i<4;i++){ %>
                     <tr>
                         <td align="right" width="20%">文件<%=i%>:</td>
                         <td align="center"><input type="file" name="file<%=i%>" size="35"></td>
                     </tr>      
                     <% } %>
                     <tr bgcolor="#B3DC38" height="30">
                         <td align="center" colspan="2">
                             <input type="submit" value="上传">
                             <input type="reset" value="重置">
                         </td>
                     </tr>
              
  </body>
</html>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值