SmartUpload文件上传及application内置对象的复习

本文介绍了使用SmartUpload组件实现文件上传功能的具体步骤,并演示了如何利用Application对象进行表单内容保存、IO操作及属性内容设置等实战案例。

一、SmartUpload文件上传

  1,编写FileuploadTest.htm页面

View Code
1 <form action="FileUploadTest04.jsp" method="post" enctype="multipart/form-data">    
2 请选择上传的图片:<input type="file" name="poto"/>
3 <input type="submit" value="上传">
4 </form>


  2,编写产生随机文件名的javaBean(ip+时间戳+3位随机数)RandName.java

package org.xiong.Rand;
import java.util.Date;
import java.util.Random;
import java.text.SimpleDateFormat;

public class RandName
{
	private String ipAddr;
	public RandName()
	{		
	}
	public RandName(String ip)
	{
		this.ipAddr=ip;
	}
	public String getTimeRandName()
	{
		StringBuffer sf=new StringBuffer();
		Random rd=new Random();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmssSSS");
		sf.append(sdf.format(new Date()));
		for(int i=0;i<3;i++)
		{
			sf.append(rd.nextInt(10));
		}
		return sf.toString();
	}
		public String getRandName()
		{
			StringBuffer sf=new StringBuffer();
			String[] temp=this.ipAddr.split("\\.");
			for(int i=0;i<temp.length;i++)
			{
				sf.append(addZero(temp[i],3));
			}	
			return sf.append(this.getTimeRandName()).toString();		
		}
		public String addZero(String s,int len)
		{
			StringBuffer sf=new StringBuffer();
			sf.append(s);
			if(s.length()<len)
			{				
				sf.insert(0,"0");
			}
			return sf.toString();
		}	
}

   3,编写FileUploadTest.jsp页面

<%@ page import="java.io.*,org.xiong.Rand.RandName"%>
<jsp:useBean id="smartupload" class="org.lxh.smart.SmartUpload"/>


<%
RandName rn
=new RandName(request.getRemoteAddr());
request.setCharacterEncoding(
"GBK");
smartupload.initialize(pageContext);
smartupload.upload();
String fileName=rn.getRandName();
String extName="."+smartupload.getFiles().getFile(0).getFileExt() ;
fileName
+=extName;
smartupload.getFiles().getFile(
0).saveAs(getServletContext().getRealPath("/")+"FileUploadTest"+File.separator+"pic"+File.separator+fileName);
%>

使用SmartUpload组件编写文件上传功能时,需要注意的是:

1.表单需要封装,enctype="multipart/form-data"。

2.封装后的表单在jsp页面中不能通过request.getParameter()得到属性参数,需要借助于SmartUpload提供的smartupload.getRequest()得到request对象。

3.可以通过smartupload.getFiles().getFile(0).getFileExt()得到所选择文件的扩展名,getFiles()方法得到所有选择的文件,然后通过getFile()得到该文件。

4.当改变选择的文件名称时候,不能再使用save()方法保存上传的文件,只能通过 getFiles().getFile(0).saveAs()方法保存。


二、Application对象

  appliaction对象属于javax.servlet.ServletContext接口的对象。可以通过getServletContext()方法获得该对象,该方法被容器调用。application对象一般常用的方法有getRealPath("/"),通过虚拟目录名称获取真实的路径,此方法使用广泛。

  

 A:练习---使用application对象保存表单提交的内容

<!--编写表单Input.htm-->
<form action="application_demo04.jsp" method="post">
	姓名:<input type="text" name="name"/><br/>
	年龄:<input type="text" name="age"/><br/>
	简介:<textarea rows="10" cols="20" name="content"></textarea><br/>
	<input type="submit" value="提交">
</form>

<!--编写处理页面Application_demo01.jsp-->
<%@ page contentType="text/html;charset=GBK"%>
<%@ page import="java.io.*,java.util.*"%>
<%
	request.setCharacterEncoding("GBK");	
%>
<jsp:useBean id="info" class="org.xiong.applicationdemo.info" scope="page" />
<jsp:setProperty name="info" property="*"/>
<%
	try
	{
		String name=info.getName();
		String age=info.getAge();
		String content=info.getContent();
		PrintStream ps=new PrintStream(new FileOutputStream(new File(this.getServletContext().getRealPath("/")+"applicationdemo"+File.separator+"content.txt")));	
		ps.println(name);
		ps.println(age);
		ps.println(content);
		ps.close();
	}
	catch(Exception ex)
	{
		ex.printStackTrace();
	}	
%>

   B:练习---操作IO

View Code
 1 <%@ page contentType="text/html;charset=GBK"%>
2 <%@ page import="java.io.*"%>
3 <%!
4 public int load(String path)
5 {
6 File file=new File(path);
7 BufferedReader br=null;
8 int temp=0;
9 try
10 {
11 br=new BufferedReader(new InputStreamReader(new FileInputStream(file)));
12 String str=br.readLine();
13 temp=Integer.parseInt(str);
14 br.close();
15 }
16 catch(Exception ex)
17 {
18 ex.printStackTrace();
19 }
20 return temp;
21 }
22 public synchronized void save(int n,String path)
23 {
24 PrintStream ps=null;
25 try
26 {
27 ps=new PrintStream(new FileOutputStream(new File(path)));
28 ps.println(n);
29 ps.close();
30 }catch(Exception ex)
31 {
32 ex.printStackTrace();
33 }
34
35 }
36 %>
37 <%
38 String pathName=this.getServletContext().getRealPath("/")+"applicationdemo"+File.separator+"coount.txt";
39 int count=load(pathName);
40 if(session.isNew())
41 {
42 save(++count,pathName);
43 }
44 %>
45 <h1>你是第<%=count%>位访客!</h1>

  C:练习---使用application.setAttribute()保存属性内容

View Code
 1 <!--显示页面-->
2 <frameset rows="80%,20%">
3 <frame name="top" src="application_demo06.jsp"/></frame>
4 <frame name="bottom" src="application_demo07.jsp"/></frame>
5 </frameset>
6
7 <!--application_demo06.jsp-->
8 <%@ page contentType="text/html;charset=GBK"%>
9 <%@ page import="java.io.*,java.util.*"%>
10 <%
11 response.setHeader("refresh","2");
12 request.setCharacterEncoding("GBK");
13 if(this.getServletContext().getAttribute("notes")==null)
14 {
15 if(session.isNew())
16 {
17 %>
18 <h1>没有留言!</h1>
19 <%
20 }
21 }
22 else
23 {
24 List list=(List)getServletContext().getAttribute("notes");
25 Iterator iter=list.iterator();
26 while(iter.hasNext())
27 {
28 %>
29 <h1>说:<%=iter.next()%></h1>
30 <%
31 }
32 }
33 %>
34
35
36 <!--application_demo07.jsp-->
37 <%@ page contentType="text/html;charset=GBK"%>
38 <%@ page import="java.util.*"%>
39 <form action="application_demo07.jsp" method="post">
40 请输入内容:<input type="text" name="content">
41 <input type="submit" value="说">
42 </from>
43 <%
44 request.setCharacterEncoding("GBK");
45 List list=null;
46 String str=(String)request.getParameter("content");
47 if(str!=null)
48 {
49 list=(List)this.getServletContext().getAttribute("notes");
50 if(list==null)
51 {
52 list=new ArrayList();
53 }
54 list.add(str);
55 this.getServletContext().setAttribute("notes",list);
56 }
57 %>




 

 

 

 



转载于:https://www.cnblogs.com/xiongyu/archive/2012/01/31/2332528.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值