jsp应用指南-实现附件上传功能
我们使用开源的Smartupload智能上传组件:
1.使用html表单的file元素可以上传:
2.常见组件:
Smartupload, Apache fileuplaod
上传的步骤:
1.初始化上传上下文;
2.准备上传;
3.保存文件;
–表单上传多媒体文件,必须封装表单:
// upload.jsp:
<%@page contentType="text/html;charset=utf8"%>
<%
smart.initialize(pageContext); // 初始化上传
smart.upload(); // 准备上传
smart.save(“upload”); // 保存文件
%>
// 但表单封装后,request将无法获取表单属性!
// 可以借助smartupload提供的方法:
<%@page contentType="text/html;charset=utf8"%>
<%
request.setCharacterEncoding(“utf8”);
smart.initialize(pageContext); // 初始化上传
smart.upload(); // 准备上传
smart.save(“upload”); // 保存文件
String username = smart.getRequest().getParameter(“user”);
%>
<%=username%>
———修改文件名后上传:
<%@page contentType="text/html;charset=utf8"%>
<%
request.setCharacterEncoding(“utf8”);
smart.initialize(pageContext); // 初始化上传
smart.upload(); // 准备上传
String name = smart.getRequest().getParameter(“user”);
name = name + “.” + smart.getFiles().getFile(0).getFileExt(); // 获取当前文件扩展名
String filename = this.getServletContext().getRealPath(“/”) + “upload/” + name; // 获取文件完整真实路径名
smart.getFiles().getFile(0).saveAs(filename); // 文件另存为修改后的名字
%>
<%@page import="java.sql.*"%>
转载请注明:转自《jsp应用指南-实现附件上传功能》
本文地址:http://www.xiupu.net/archives-198.html
关注公众号:
赞赏
微信赞赏
支付宝赞赏