JSP中实现文件上传--SmartUpload

SmartUpload上传组件:

准备前提:smartupload.jar

要想进行上传,则必须使用HTML中提供的file控件,而且<form>也必须使用enctype属性进行封装。

smartupload.html

<html>
 <head>
  <title>smartupload Test</title>
 </head>
 <body>
	<form action="smartupload.jsp" method="post" enctype="multipart/form-data">
		请选择文件:<input type="file" name="pic">
		<input type ="submit" value="上传">
	</form>
 </body>
</html><span style="font-weight: bold; ">
</span>
*表单中使用了file控件进行文件的选择,而且在form上使用enctype进行了表单封装,表示表单将按照二进制的方式提交,

即所有的操作表单此时不再是分别提交,而是将所有内容都按照二进制的方式提交。

smartupload.jsp

接收图片,保存在根目录中的upload文件夹下:

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="org.lxh.smart.*"%>
<html>
 <head>
  <title>smartupload Test</title>
 </head>
 <body>
<%
	SmartUpload smart = New SmartUpload();
	smart.initialize(pageContext);
	smart.upload();
	smart.save("upload");
%>
 </body>
</html><span style="color:#000099;font-weight: bold; ">
</span>
混合表单

smartupload2.html

<span style="font-size:12px;"><html>
 <head>
  <title>smartupload Test</title>
 </head>
 <body>
	<form action="smartupload2.jsp" method="post" enctype="multipart/form-data">
		姓名:<input type="text" name="uname"><br>
		照片:<input type="file" name="pic"><br>
		<input type ="submit" value="上传">
		<input type="reset" value="重置">
	</form>
 </body>
</html></span><span style="font-size:18px;font-weight: bold; ">
</span>
接收封装表单的文本数据:smartupload2.jsp

<span style="font-size:12px;"><%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="org.lxh.smart.*"%>
<html>
 <head>
  <title>smartupload Test</title>
 </head>
 <body>
 <%
	request.setCharacterEncoding("GBK"); //解决乱码
 %>
<%
	SmartUpload smart = New SmartUpload(); //实例化SmartUpload上传组件
	smart.initialize(pageContext); //初始化上传操作
	smart.upload(); //上传准备
	String name = smart.getRequest().getParameter("uname"); //接收请求参数
	smart.save("upload"); //将上传文件保存在upload文件夹中
%>
<h2>姓名:<%=name%></h2>
<h2>request无法取得:<%=request.getParameter("uname")%></h2>
 </body>
</html></span><span style="font-size:18px;font-weight: bold; ">
</span>
*由于表单进行了二进制封装,所以单纯靠request对象是无法取得提交参数的,

必须依靠SmartUpload类中的getRequest().getParameter()方法才能取得请求的参数。

批量上传
编写表单可以上传三个文件:smartupload3.html

<html>
 <head>
  <title>smartupload Test</title>
 </head>
 <body>
	<form action="smartupload3.jsp" method="post" enctype="multipart/form-data">
		照片1:<input type="file" name="pic1"><br>
		照片2:<input type="file" name="pic2"><br>
		照片3:<input type="file" name="pic3"><br>
		<input type ="submit" value="上传">
		<input type="reset" value="重置">
	</form>
 </body>
</html>
smartupload3.jsp

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="org.lxh.smart.*"%>
<%@ page import="IPTimeStamp"%><!--定义一个IP时间戳的操作类,为了重命名上传的文件-->
<html>
 <head>
  <title>smartupload Test</title>
 </head>
 <body>
 <%
	request.setCharacterEncoding("GBK"); //解决乱码
 %>
<%
	SmartUpload smart = New SmartUpload(); //实例化SmartUpload上传组件
	smart.initialize(pageContext); //初始化上传操作
	smart.upload(); //上传准备
	IPTimeStamp its = New IPTimeStamp(request.getRemoteAddr()); //实例化IPTimeStamp对象
	For(int x=0;x<smart.getFiles().getCount();x++){
		String ext = smart.getFiles().getFile(x).getFileExt(); //取得文件后缀
		String fileName = its.getIPTimeRand()+"."+ext; //拼凑文件名称
		smart.getFiles().getFile(x).saveAs(getServletContext().getRealPath("/")+"upload"+java.io.File.separator+fileName); //保存文件
	}
%>
 </body>
</html>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
package com.etoak.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.etoak.dao.UploaderDaoIf; import com.etoak.dao.UploaderDaoImpl; import com.etoak.po.Uploader; import com.etoak.util.UUIDGenerator; import com.jspsmart.upload.File; import com.jspsmart.upload.Files; import com.jspsmart.upload.Request; import com.jspsmart.upload.SmartUpload; public class Upload extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=gbk"); request.setCharacterEncoding("gbk"); try { // 1:引入smartupload SmartUpload su = new SmartUpload(); // 2:设置允许上传的文件的后缀名,用逗号隔开 su.setAllowedFilesList("jpg,gif,bmp,jpeg"); // 3:设置允许上传文件的大小 .这里是3m su.setMaxFileSize(3 * 1024 * 1024); // 4:初始化,接受页面传递过来的请求 su.initialize(getServletConfig(), request, response); // 5:上传 su.upload(); // 拿取Smartupload的request // 注意当我们使用了SmartUpload此jar包后 // 无法再次使用httpServletRequest这个对象来调用 // getParameter这个方法了 Request myreq = su.getRequest(); String name = myreq.getParameter("name"); String pass = myreq.getParameter("pass"); // 拿取所有上传文件的对象 Files files = su.getFiles(); // 拿取我们上传的唯一一个文件 // 0 表示索引值 File file = files.getFile(0); // 上传文件的名字 String fileName = file.getFileName(); // 上传文件的大小 int fileSize = file.getSize(); // 上传文件的后缀名 String fileExt = file.getFileExt(); // 在服务器端开辟一个路径,建立文件夹放置文件 java.io.File myfile = new java.io.File(this.getServletContext() .getRealPath("/image")); // 如果不存在此路径 if (!myfile.exists()) { // 建立此路径 myfile.mkdir(); } // 58495849584954895.jpg String fileTrueName = new UUIDGenerator().generate() + "." + fileExt; // 组合一个另存为路径 // /image/43894834830430.jpg String finalPath = "/image/" + fileTrueName; // 另存为 file.saveAs(finalPath); Uploader up = new Uploader(); up.setName(name); up.setPass(pass); up.setPicPath(finalPath); UploaderDaoIf dao = new UploaderDaoImpl(); boolean flag = dao.addUp(up); if (flag) { this.getServletContext().setAttribute("up", up); response.sendRedirect("/JspDay3_upload/show.jsp"); } } catch (Exception ex) { ex.printStackTrace(); } } }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值