Pioneer.HengYu

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

一.图片上传:

                 FileUpload是用来实现Java环境下的文件上传功能,与常见的SmartUpload齐名

客户端:  

      1.form的enctype属性设置为 multipart/form-data,设置该值后,浏览器在上传文件时,将把文件数据附带在Http请求体中,并使用MIME协议对上传文件进行描述,以方便接收方对上传数据进行解析和处理。

      2.form的method属性设置为 POST
      3.添加文件上传项<input type=“file” ... />

上传步骤:( smartupload)

   1.引入jar文件

 jspsmartupload.jar

      2.实例化上载类

 SmartUpload mySmartUpload = new SmartUpload();

      3.初始化  (pageContext)

  mySmartUpload.initialize(pageContext);

      4.准备上传  (没有直接上传呢)

    mySmartUpload.upload();

      5.保存上传文件  (提交)   

  mySmartUpload.save("/upup");

 

 

•需求将一张图片上传到服务器的upup文件夹下

 文件路径

创建表单上传页面

 1 indexTest.jsp表单上传页面
 2 
 3 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 4 <%
 5 String path = request.getContextPath();
 6 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 7 %>
 8 
 9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
10 <html>
11   <head>
12     <base href="<%=basePath%>">
13     
14     <title>test上传</title>
15     <meta http-equiv="pragma" content="no-cache">
16     <meta http-equiv="cache-control" content="no-cache">
17     <meta http-equiv="expires" content="0">    
18     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
19     <meta http-equiv="description" content="This is my page">
20     <!--
21     <link rel="stylesheet" type="text/css" href="styles.css">
22     -->
23   </head>
24   
25   <body>
26                         <!--  上传的表单要这样被封装    -->
27   <form action="up.jsp" method="post" enctype="multipart/form-data"/>
28     请输入:<input type="text" name="name"/><br/>
29     <input type="file" name="file"/>
30     <input type="submit" value="上传">
31   
32   </form>
33   </body>
34 </html>
 1 上传流程
 2 
 3 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 4 <%@ page import="com.jspsmart.upload.*"%>
 5 
 6 
 7 <%
 8     String path = request.getContextPath();
 9     String basePath = request.getScheme() + "://"
10             + request.getServerName() + ":" + request.getServerPort()
11             + path + "/";
12 %>
13 
14 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
15 <html>
16     <head>
17         <base href="<%=basePath%>">
18 
19         <title>My JSP 'up.jsp' starting page</title>
20 
21         <meta http-equiv="pragma" content="no-cache">
22         <meta http-equiv="cache-control" content="no-cache">
23         <meta http-equiv="expires" content="0">
24         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
25         <meta http-equiv="description" content="This is my page">
26         <!--
27     <link rel="stylesheet" type="text/css" href="styles.css">
28     -->
29 
30     </head>
31 
32     <body>
33         <%
34             //步骤一:实例化上载bean
35             SmartUpload mySmartUpload = new SmartUpload();
36 
37             //步骤二:初始化
38             mySmartUpload.initialize(pageContext);
39                 
40             //步骤三:准备上载文件
41                 mySmartUpload.upload();
42                 
43             //步骤四:保存文件
44             //    mySmartUpload.save("/upup");
45             
46 ////////////////////////////////////////////////////////////////////    
47             //表单被封装后直接通过request不能获得值    
48             //    String name=request.getParameter("name");
49         String name=mySmartUpload.getRequest().getParameter("name");
50 
51           //取得文件扩展名,后缀
52           String ext=mySmartUpload.getFiles().getFile(0).getFileExt();
53            
54          //利用自定义名字上传,保存文件名  getFiles()得到所有file类型数组,可用作批量上传  savaAs()方法用来更改名字
55         // mySmartUpload.getFiles().getFile(0).saveAs("/upup/"+name+"."+ext);
56 
57            //利用随机数名字上传,保存文件名
58            String uuid = UUID.randomUUID().toString();
59           mySmartUpload.getFiles().getFile(0).saveAs("/upup/"+uuid+"."+ext);
60         %>
61 
62         <h1>上传成功</h1>
63         
64     </body>
65 </html>
  1 <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
  2 <%@ page import="java.util.*" %>
  3 <%@ page import="java.text.SimpleDateFormat" %>
  4 <%@ page import="com.jspsmart.upload.*" %>
  5 
  6 
  7 <% 
  8 response.setHeader("Expires","0");
  9 response.setHeader("Pragma","nono-catch");
 10 response.setHeader("Catch-Control","no-cache");
 11 %> 
 12 
 13 <% 
 14     String _contexPath = request.getContextPath();
 15     String ext = "";
 16   
 17 %> 
 18 
 19 <html>
 20 <head>
 21 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 22 <script language="JavaScript" src="<%=_contexPath%>/css/common.js"></script>
 23 <title>……</title>
 24 </head>
 25 <body> 
 26 <%
 27         
 28         String trace = "";
 29         String filename = "";
 30         try
 31         {
 32                  //实例化上载bean
 33                 SmartUpload mySmartUpload = new SmartUpload();
 34                 
 35                 //初始化
 36                 mySmartUpload.initialize(pageContext);
 37                 
 38                 // 设定上传限制
 39                 // 1.限制每个上传文件的最大值
 40                 mySmartUpload.setMaxFileSize(500 * 1024 * 1024 * 1024);
 41                 // 2.限制总上传数据的长度。
 42                 //mySmartUpload.setTotalMaxFileSize(200000000);
 43                 // 3.设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。
 44                 //mySmartUpload.setAllowedFilesList("doc,txt");
 45                 // 4.设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat,jsp,htm,html扩展名的文件和没有扩展名的文件。
 46                 //mySmartUpload.setDeniedFilesList("exe,bat,jsp,htm,html,,");
 47                 
 48                 //上载文件
 49                 mySmartUpload.upload();
 50                 // 将上传文件全部保存到指定目录
 51                 //int count = mySmartUpload.save("/upload");
 52                 //System.out.println(mySmartUpload.getFiles().getCount() + "个文件上传成功!<br>");
 53                 
 54                 //获得Request对象
 55                 //re1 = (HttpServletRequest)pageContext.getRequest();
 56                 //re1 = (HttpServletRequest)mySmartUpload.getRequest();
 57                 //String other = mySmartUpload.getRequest().getParameter("other");
 58                 
 59                        
 60                 //循环取得所有上载的文件
 61                 
 62                 for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++)
 63                 {
 64                       //取得上载的文件
 65                       com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
 66                       
 67                       if (!myFile.isMissing())
 68                     {
 69                        ext = myFile.getFileExt();
 70                        //全名(绝对路径+名称)
 71                        String myFilePath = myFile.getFilePathName();
 72                        //取得文件的大小
 73                        int fileSize = myFile.getSize();
 74                        filename = new String(myFile.getFileName().getBytes(),"utf-8");
 75                       // System.out.print(filename);
 76                        //另存路径
 77                        //String jspPath = application.getRealPath(request.getRequestURI());//本JSP文件的服务器绝对路径
 78                        String filePath = getServletContext().getRealPath("/imgupload/");//本WEB程序根目录服务器绝对路径 
 79                        
 80                        //filePath += "\\UserFiles\\Image\\";
 81                     
 82                        
 83                        //要创建的文件最终保存目录
 84                        java.io.File file = new java.io.File( filePath ); 
 85                        
 86                       // System.out.println("filepath==="+filePath);
 87                          
 88                          if(file.exists())
 89                                      file.mkdirs();
 90                                      
 91   
 92                      //  trace += ((new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()))+"."+ext);//最终保存路径
 93                        
 94                        
 95                       // request.setAttribute("Image",trace);
 96                       // System.out.println("trace==="+filename);
 97                        
 98                        myFile.saveAs(filePath + "\\" + filename);
 99 
100                        // 将文件另存
101                        //myFile.saveAs("\\upload\\" + myFile.getFileName());
102                        // 另存到以WEB应用程序的根目录为文件根目录的目录下
103                        //myFile.saveAs("\\upload\\" + myFile.getFileName(), SmartUpload.SAVE_VIRTUAL);
104                        //将文件另存到指定目录(操作系统的目录)
105                        //myFile.saveAs(trace, SmartUpload.SAVE_PHYSICAL);
106                       } 
107                       else
108                       {
109                           // 若文件不存在则继续
110                         continue;
111                         
112                       }
113                 }//end for
114           }catch(Exception SE){
115             System.out.println("com.jspsmart.upload.SmartUpload上传期间产生错误!");
116           }
117 %>
118 <script language='javascript'>
119 alert('上传成功');
120 </script>
121 <img src="imgupload/<%=filename %>">
122 </body>
123 </html>

运行结果

 

posted on 2017-03-31 16:43  Pioneer.HengYu  阅读(143)  评论(0编辑  收藏  举报