基于jspSmartUpload的Jsp文件上传:防止文件重名被覆盖的解决方法

基于jspSmartUpload的Jsp文件上传:防止文件重名被覆盖的解决方法

支持中文的jsp文件上传组件:jspSmartUpload.jar下载

upload_repeat.html

<html>
<head>
<title>中国网页设计:文件上传</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>

<body>
<p align="center">上传文件选择</p>
<FORM METHOD="POST" ACTION="repeat_upload.jsp" ENCTYPE="multipart/form-data">
  <table width="75%" border="1" align="center">
    <tr> 
      <td><div align="center">1、 
          <input type="FILE" name="FILE1" size="30">
        </div></td>
    </tr>        
    <tr> 
      <td><div align="center">
          <input type="submit" name="Submit" value="上传它!">
        </div></td>
    </tr>
  </table>
</FORM>
</body>
</html>

repeat_upload.jsp

<%@ page contentType="text/html; charset=gb2312"  import="com.jspsmart.upload.SmartUpload,java.util.*,java.text.SimpleDateFormat" %>
<html>
<head>
<title>文件上传处理页面</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<%
 // 新建一个SmartUpload对象
 SmartUpload su = new SmartUpload();
 // 上传初始化
 su.initialize(pageContext);
 // 上传文件
 su.upload();
  //创建File类的对象以获取上传文件的信息
  out.print("文件上传成功<br>");
  out.print("上传文件的信息:<br>"); 
     com.jspsmart.upload.File file = su.getFiles().getFile(0); 
  Date dt=new Date();
//如果不需要格式,可直接用dt,dt就是当前系统时间
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
//设置显示格式
String nowTime= df.format(dt);
file.saveAs("/upload/" +nowTime+"_"+ file.getFileName(), su.SAVE_VIRTUAL);
out.println("文件名:" +nowTime+"_"+ file.getFileName() + "<br />");  
%>
</body>
</html>

相关文章:基于jspSmartUpload的Jsp文件上传实例

125jz网原创文章。发布者:江山如画,转载请注明出处:http://www.125jz.com/2919.html

(3)
江山如画的头像江山如画管理团队
上一篇 2018年12月11日 下午2:57
下一篇 2018年12月11日 下午3:14

99%的人还看了以下文章

发表回复

登录后才能评论