jspsmart 上传附件到服务器

jspsmart 上传附件到服务器

index.jsp

 1 <% @ page contentType = " text/html;charset=gb2312 " %>
 2 < html >
 3      < head >
 4          < title > 测试上传附件 </ title >
 5          < metahttp - equiv  = " Content-Type " content = " text/html;charset=GB2312 " >
 6      </ head >
 7      < body >
 8          < h2 >
 9             测试上传附件
10          </ h2 >
11          < form name = " Form1 "  enctype = " multipart/form-data "  method = " post "  action = " Jspsmart.jsp " >
12              < p >
13                 上传文件1:
14                  < input type = " file "  name = " File1 "  size = " 20 "  maxlength = " 20 " >
15              </ p >
16              < p >
17                 上传文件2:
18                  < input type = " file "  name = " File2 "  size = " 20 "  maxlength = " 20 " >
19              </ p >
20              < input type = " submit "  value = " 上传 " >
21              < input type = " reset "  value = " 清除 " >
22          </ form >
23      </ body >
24 </ html >

Jspsmart.jsp
 1 <% @ page  import = " com.jspsmart.upload.* " %>
 2 <% @ page contentType = " text/html;charset=GB2312 " %>
 3
 4 < html >
 5      < head >
 6          < title > CH9  -  Jspsmart2.jsp </ title >
 7      </ head >
 8      < body >
 9
10          < h2 >
11             文件上传范例  -  jspSmart
12          </ h2 >
13
14          < jsp:useBean id = " mySmartUpload "  scope = " page "   class = " com.jspsmart.upload.SmartUpload "   />
15          <%
16              // 计算文件上传个数
17              int  count  =   0 ;
18              try   {
19                //SmartUpload的初始化,使用这个jspsmart一定要在一开始就这样声明
20                mySmartUpload.initialize(pageContext);
21
22                //限制每个上传附件的最大长度。 
23                mySmartUpload.setMaxFileSize(5000000);
24
25                //限制总上传数据的长度。 
26                mySmartUpload.setTotalMaxFileSize(10000000);
27
28                //设定允许上传的附件(通过扩展名限制)。 
29                mySmartUpload.setAllowedFilesList("jpg,gif,GIF,JPG");
30
31                //依据form的内容上传
32                mySmartUpload.upload();
33
34                //将上传的文件一个一个取出来处理
35                for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++{
36                    //取出一个文件
37                    com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
38
39                    //如果文件存在,则做存档操作
40                    if (!myFile.isMissing()) {
41
42                //将文件存放于绝对路径的位置
43                myFile.saveAs("C:\\upload\\" + myFile.getFileName(),mySmartUpload.SAVE_PHYSICAL);
44
45                //显示此上传文件的详细信息
46                out.println("FieldName = " + myFile.getFieldName() + "<BR>");
47                out.println("Size = " + myFile.getSize() + "<BR>");
48                out.println("FileName = " + myFile.getFileName() + "<BR>");
49                out.println("FileExt = " + myFile.getFileExt() + "<BR>");
50                out.println("FilePathName = " + myFile.getFilePathName() + "<BR>");
51                out.println("ContentType = " + myFile.getContentType() + "<BR>");
52                out.println("ContentDisp = " + myFile.getContentDisp() + "<BR>");
53                out.println("TypeMIME = " + myFile.getTypeMIME() + "<BR>");
54                out.println("SubTypeMIME = " + myFile.getSubTypeMIME() + "<BR>");
55                count++;
56                    }

57                }

58
59                // 显示应该上传的文件数目
60                out.println("<BR>" + mySmartUpload.getFiles().getCount() + " files could be uploaded.<BR>");
61
62                // 显示成功上传的文件数目
63                out.println(count + "file(s) uploaded.");
64            }
  catch  (SmartUploadException e)  {
65                System.out.println("上传文件出错");
66                e.getMessage();
67            }

68          %>
69      </ body >
70 </ html >

你可能感兴趣的:(jspsmart 上传附件到服务器)