第十六天前端jsp文件上传和下载及servlet的使用

二十文件上传及下载

加入SmartUpload jar包,专门用于实现文件上传及下载的组件

1.文件上传

public final void initialize(PageContextpageContext) 执行上传和下载的初始化工作,必须实现
public void upload() 实现文件数据的 传 , 在initialize 方法后执行
public int save(String pathName) 将全部上传文件保存到指定的目录下,并返回保存的文件个数
public void setAllowFilesList(String ExtList) 指定允许上传的文件扩展名,接收一个扩展名列表,以逗号分隔
public void setDeniedFilesList(String fileList) 指定了禁止上传的文件扩展名列表,每个扩展名之间以逗号分隔
public void setMaxFileSize(long filesize) 设定每个文件允许上传的最大长度
public void setTotalMaxFileSIze(long totalfilesize) 设定允许上传文件的总长度

Files
封装了所有上传文件的信息集合
public int getCount() 取得文件上传的数目

public long getSize() 取得上传文件的总长度

public File getFile(int index) 取得指定位置的 File 文件对

File

封装了单个上传文件所包含的信息

saveAs(String destFilePathName)将文件保存,参数destFilePathName 是保存的文件名
public String getFiledName( )获取表单中当前上传文件所对应的表单项的名称
public String getFileName( ) 获取上传文件的文件名称,不包含路径

<%
	out.print("reqContextPath:"+request.getContextPath()+"<br/>");
	out.print("reqServletPath:"+request.getServletPath()+"<br/>");
	out.print("reqURL:"+request.getRequestURI()+"<br/>");
	out.print("reqRealPath:"+request.getRealPath("/")+"<br/>");
%>
<form method="post" action="S2upload02upload.jsp"  enctype= "multipart/form-data">
上传文件1:<input type="file" name="filename"><br/>
上传文件2:<input type="file" name="filename"><br/>
上传姓名:<input type="text" name="filename"><br/>
<input type="submit" value="提交"> 
</body>
</html>

ContextPath:/javaWeb	
ServletPath:/1101updownload/S1upload02.jsp	
URL:/javaWeb/1101updownload/S1upload02.jsp
RealPath:E:\workspace\woekspacejava\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\javaWeb\
S2upload02upload.jsp

<body>
<%	//设置字符编码
	request.setCharacterEncoding("utf-8");
	//得到smartupload对象
	SmartUpload upload=new SmartUpload();
	//上传初始化
	upload.initialize(pageContext);
	//设置单个文件最大长度
	upload.setMaxFileSize(1000000);
	//设置总文件的最大长度
	upload.setTotalMaxFileSize(10000000);
	//文件上传
	upload.upload();
	//上传文件保存路径
	//int k=upload.save("/upload");
	//out.print("文件上传个数:"+k+"<br/>");
	
	Files files=upload.getFiles();
	out.print(files.getCount()+"<br/>");
	out.print(files.getSize()+"<br/>");
	for(int i=0;i<files.getCount()-1;i++){	out.print("ssss:"+files.getFile(i).getFieldName()+"\t"+files.getFile(i).getFilePathName()+"\t"+files.getFile(i).getSize()+"\t"+"<br/>");
		String filename=files.getFile(i).getFilePathName()+"."+files.getFile(i).getFileExt();
		out.println("filename:"+filename+"<br/>");
		files.getFile(i).saveAs("/upload/"+filename);	
	}	
%>
</body>
2.文件下载
<body>
<%
//设置字符编码
	request.setCharacterEncoding("utf-8");
	//得到smartupload对象
	SmartUpload download=new SmartUpload();
	//上传初始化
	download.initialize(pageContext);
	//设置单个文件最大长度
	download.setMaxFileSize(1000000);
	//设置总文件的最大长度
	download.setTotalMaxFileSize(10000000);
	//防止浏览器自动打开图片
		download.downloadFile("/upload/362525199807161534.jpg");
	Files files=download.getFiles();

%>
</body>

二十一servlet

1.servlet处理步骤

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-irMGt3nw-1604245373120)(C:\Users\lgm\AppData\Roaming\Typora\typora-user-images\1604240055297.png)]

  1. 客户端(web浏览器)通过HTTP提出请求,WEB服务器接收该请求并将其发送给servlet

  2. servlet程序将接收该HTTP请求并执行某种处理

  3. servlet会将处理后的结果向Web服务器返回应答,WEB服务器将从Servlet收到的应答发回给客户端

2.servlet程序

servlet最重要的就是Servlet接口,使用时用户自定义的Servlet类都要继承HttpServlet类,重写doGet()或doPost()方法

常用方法

  1. public void init() Servlet 初始化时调用

  2. public void doGet(HttpServletRequest request,HttpServletRresponse response) 以 Get 请求服务时调用

  3. public void doPost(HttpServletRequest request,HttpServletRresponse response) 以 Post 请求服务时调用

  4. public abstract voidservice(ServletRequestreq,ServletResponse res) 一般不会直接覆写
    此方法, 而是使用doGet() 或doPost()方法

  5. public void destroy() 普通 Servlet 销毁时调用

3.servlet生命周期

Servlet程序时运行在服务端的一段java程序,其声明周期收到WEB容器的控制

加载程序(创建servlet实例)–>初始化(init())–>服务(doGet()/doPost())–>销毁(destory())–>卸载

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0rq8kdTL-1604245373125)(C:\Users\lgm\AppData\Roaming\Typora\typora-user-images\1604240579088.png)]

4.使用Servlet
<body>
<a href="servlet">simpleServlet</a>
<br/>
</body>

web.xml

	<servlet>
		<servlet-name>simpleServlet</servlet-name>
		<servlet-class>S1101Servlet.S1SimpleServlet</servlet-class>
		<init-param>
		<param-name>driver</param-name>
		<param-value>com.mysql.jdbc.Driver</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>simpleServlet</servlet-name>
		<url-pattern>/1101servlet/servlet</url-pattern>
	</servlet-mapping>

S1101Servlet.S1SimpleServlet

package S1101Servlet;
public class S1SimpleServlet extends HttpServlet{
	String driver=null;
	@Override
	public void init(ServletConfig config) throws ServletException {
		System.out.println("servlet初始化,且可用servletConfig读取配置信息");
		driver=config.getInitParameter("driver");
		String url=config.getInitParameter("url");
		String root=config.getInitParameter("root");
		String password=config.getInitParameter("password");	System.out.println("driver:"+driver+"\t"+"url:"+url+"\troot:"+root+"\tpassword:"+password);
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html;charset=utf-8");
		System.out.println("访问Servlet");
		System.out.println(req.getContextPath()+"/1101servlet/S2simpleServlet.jsp");
		req.getRequestDispatcher("/1101servlet/S2simpleServlet.jsp").forward(req, resp);
	}
	@Override
	public void destroy() {
		System.out.println("销毁servlet");
	}
}
5.获得HttpSession对象

通过HttpServletRequest的

public HttpSession getSession()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值