天空是蓝色的

做好软件为中国 #gcc -c helloworld.c -o helloworld.o //编译目标文件 #gcc helloworld.o -o helloworld //编译成可执行exe #helloworld //运行exe
数据加载中……
运行上传组件例子Upload Component

创建3个文件
UploadPage.java

  1import java.io.File;
  2import java.io.FileOutputStream;
  3import java.io.IOException;
  4import java.io.InputStream;
  5import java.text.Format;
  6import java.text.NumberFormat;
  7
  8import org.apache.tapestry.IRequestCycle;
  9import org.apache.tapestry.html.BasePage;
 10import org.apache.tapestry.request.IUploadFile;
 11
 12public class UploadPage extends BasePage {
 13    public static final Format SIZE_FORMAT = NumberFormat.getNumberInstance();
 14
 15    private IUploadFile file;
 16
 17    private File serverFile;
 18
 19    public IUploadFile getFile() {
 20        return file;
 21    }

 22
 23    public void setFile(IUploadFile value) {
 24        file = value;
 25    }

 26
 27    public String getFilename() {
 28        if (file != null{
 29            return file.getFileName();
 30        }
 else {
 31            return "";
 32        }

 33    }

 34
 35    public String getClientPath() {
 36        if (file != null{
 37            return file.getFilePath();
 38        }
 else {
 39            return "";
 40        }

 41    }

 42
 43    public String getServerPath() {
 44        if (serverFile != null{
 45            return serverFile.getAbsolutePath();
 46        }
 else {
 47            return "";
 48        }

 49    }

 50
 51    public long getFileSize() {
 52        if (serverFile != null{
 53            return serverFile.length();
 54        }
 else {
 55            return 0;
 56        }

 57    }

 58
 59    public boolean isFileTruncated() {
 60        if (file != null{
 61            return true;
 62            //修改过
 63        }
 else {
 64            return false;
 65        }

 66    }

 67
 68    public void formSubmit(IRequestCycle cycle) {
 69        InputStream fis = file.getStream();
 70        FileOutputStream fos = null;
 71
 72        try {
 73            fos = new FileOutputStream(new File(file.getFileName()));
 74            byte[] buffer = new byte[1024];
 75            while (true{
 76                int length = fis.read(buffer);
 77                if (length < 0{
 78                    break;
 79                }

 80                fos.write(buffer, 0, length);
 81            }

 82            fis.close();
 83            fos.close();
 84            serverFile = new File(file.getFileName());
 85
 86        }
 catch (IOException ioe) {
 87            ioe.printStackTrace();
 88        }
 finally {
 89            if (fis != null{
 90                try {
 91                    fis.close();
 92                }
 catch (IOException ioe) {
 93                }

 94            }

 95            if (fos != null{
 96                try {
 97                    fos.close();
 98                }
 catch (IOException ioe) {
 99                }

100            }

101        }

102    }

103
104/****
105    public static void main(String args[]) {
106
107    }
108**/

109    protected void initialize() {
110        file = null;
111        serverFile = null;
112    }

113}

114

UploadPage.page
 1<?xml version="1.0" encoding="UTF-8"?>
 2<!DOCTYPE page-specification PUBLIC
 3  "-//Apache Software Foundation//Tapestry Specification 3.0//EN"
 4  "http://jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd">
 5<!-- generated by Spindle, http://spindle.sourceforge.net -->
 6
 7<page-specification class="UploadPage">
 8
 9    <description>add a description</description>
10    
11    
12    
13</page-specification>

UploadPage.html

 1<html>
 2<head>
 3</head>
 4<body>
 5<form jwcid="@Form" listener="ognl:listeners.formSubmit">
 6 <table bgcolor="#c0c0c0" cellpadding="4">
 7  <tr>
 8   <td colspan="2">File:&nbsp;<input jwcid="@Upload" file="ognl:file" type= "file"></input></td>
 9  </tr>
10  <tr>
11   <td colspan="2"><input type= "submit"value="Upload"></input></td>
12  </tr>
13  <tr>
14   <td colspan="2"><hr></td>
15  </tr>
16  <tr>
17   <td>Filename:</td><td><span jwcid="@Insert" value="ognl:filename"/></td>
18  </tr>
19  <tr>
20   <td>Client path:</td><td><span jwcid="@Insert" value="ognl:clientPath"/></td>
21  </tr>
22  <tr>
23   <td>Server Path:</td><td><span jwcid="@Insert" value="ognl:serverPath"/></td>
24  </tr>
25  <tr>
26   <td>File Truncated:</td><td><span jwcid="@Insert" value="ognl:fileTruncated"/></td>
27  </tr>
28  <tr>
29   <td>File Size:</td><td><span jwcid="@Insert" value="ognl:fileSize" format="ognl:@UploadPage@SIZE_FORMAT"/>&nbsp;bytes</td>
30  </tr>
31 </table>
32</form>
33</body>
34</html>

posted on 2005-11-09 10:48 bluesky 阅读(600) 评论(0)  编辑  收藏 所属分类: 框架应用


只有注册用户登录后才能发表评论。


网站导航: