考研结束了(不得不说,专业课嗨到起飞,数学小哥太菜了),小哥我又回来了,今天分享Smartupload自定义文件名的文件上传
利用Smartupload上传文件的主要步骤
(先导入所需要的jar包:jspSmartUpload.jar)
1) 创建SmartUpload类对象
2) 初始化
3) 准保存
4) 保存案例如下
jsp中简单内容如下所示
<body>
<form action="upload2.do?flag=upload2" method="post" enctype="multipart/form-data">
<input type="file" name="file2"><br/>
<input type="file" name="file3"><br/>
<input type="file" name="file4"><br/>
<input type="submit" value="上传">
</form>
</body>
public void upFile2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//创建对象
SmartUpload smart = new SmartUpload();
//初始化
smart.initialize(getServletConfig(), request, response);
try {
//准保存
smart.upload();
//保存
//smart.save("/uploadfile");//缺点:不能自定义文件名称
//smartupload的自定义文件名
SmartFiles files = smart.getFiles();
for(int i=0;i<files.getCount();i++) {
SmartFile file = files.getFile(i);
file.saveAs("/uploadfile"+File.separator+UUID.randomUUID().toString()+file.getFileName());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
<servlet>
<servlet-name>FileUpController</servlet-name>
<servlet-class>it.haikang.controller.FileUpController</servlet-class>
<!-- <multipart-config/> -->
</servlet>
<servlet-mapping>
<servlet-name>FileUpController</servlet-name>
<url-pattern>/upload.do</url-pattern>
</servlet-mapping>
xml文件如上显示
注意一个坑:在使用这种方法时候,下面的标签一定要删除了,因为这是个原生servlet文件上传的标签
<multipart-config/>
为了案例的演示,小哥将所上传的文件放到了服务器的文件夹中。这里就不展示了。
留个言,撩下小哥呗