第二篇  文件上传
文件上传也是Web开发中很常见的应用了,常见的两种方法: 
一。采用HTML标记<input type="file" name="theFile" size="30" value=""> 
  
     通常我们在后台可以选择组件,如SmartUpload等,在Struts框架中,Struts采用了Common-fileupload包,可以很方便的实现上传。 
      如下图的是前台页面: 
  
  
后台处理的Action如下: 
    
package maoxiang.examples.web.actions; 
  
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
  
import maoxiang.common.web.GenericAction; 
import maoxiang.examples.web.forms.UploadForm; 
  
import org.apache.struts.upload.FormFile; 
  
/** 
* @struts.action path="/upload/upload"  
name="UploadForm" 
* 
scope="request" 
  
input="/upload/upload1.jsp" 
* 
  
  
  
validate="true" 
*  
*/ 
public final class UploadAction extends GenericAction { 
  
protected boolean action() { 
if ("upload".equals(getAction())) { 
log.info("I am in Upload"); 
try { 
UploadForm upload = (UploadForm) form; 
  
//得到上传的文件 
FormFile file = upload.getTheFile(); 
//将它保存到/examples/temp 
//String contextPath = getServlet().getServletContext().getRealPath( 
// 
  
"/examples/upload"); 
//将文件写入到temp文件里 
  
InputStream inStream = file.getInputStream(); 
  
File temp = File.createTempFile("temp", String.valueOf(System 
.currentTimeMillis())); 
log.info((temp.getAbsolutePath())); 
FileOutputStream outStream = new FileOutputStream(temp); 
  
int bytesRead = 0; 
byte[] buffer = new byte[8192]; 
long length = 0; 
  
while ((bytesRead = inStream.read(buffer)) != -1) { 
outStream.write(buffer, 0, bytesRead); 
length += bytesRead; 
} 
  
inStream.close(); 
outStream.close(); 
request.getSession().setAttribute("para1", upload.getPara1()); 
request.getSession().setAttribute("file", temp.getAbsolutePath()); 
return true; 
} catch (IOException e) { 
log.error("上传文件时发生错误" + e.getMessage()); 
return false; 
} 
  
} 
return false; 
} 
} 
  
看上去,他和一般的Action没有什么差别,十分方便。 
  
二. 专门的上传工具 
  
由于采用Html标记,很多功能无法做到,如在上传前对文件进行限制,只能上传图片,大小只能小于2M。采用Html标记只能上传到后台后才能判断和提示,显然效率不高。 
如下是在SWT FileViewer 基础上制作的上传的工具: 
 
    
当然,采用这种方式也有一些不便之处: 
1。要求每个用户都下载这个程序,如何安装 
2。程序如果更新了,如何同步 
  
在Java环境中,这两个问题可以由Java Web Start技术解决。 
在页面上放一个启动程序的连接(符合JNLP协议),当客户点击这个连接的时候,Java Web Start就会下载该程序,如果已经下载,则比较是否由更新的,若已经更新,则从本地启动。 
JNLP如下: 
<?xml version="1.0" encoding="utf-8"?> 
<jnlp spec="1.0+" codebase="http://localhost:8080/fileviewer" href="index.jnlp"> 
<information> 
<title>File Viewer</title> 
<vendor>IBM developerWorks</vendor> 
<homepage href="index.html"/> 
<description>File Viewer</description> 
<description kind="short">File Viewer</description> 
</information> 
<security> 
<all-permissions/> 
</security> 
<resources> 
<j2se version="1.5"/> 
<jar href="fileviewer.jar"/> 
<nativelib href="swt-lib.jar"/> 
</resources> 
<resources os="Windows">  
<jar href="swt.jar"/> 
</resources> 
<application-desc main-class="org.eclipse.swt.examples.fileviewer.FileViewer"/> 
</jnlp> 
  
关于如何使用Java Web Start来启动SWT的程序,可以到IBM-900上查询相关文章。 
  
  
三. 总结 
 这两种上传方式各有有缺点。值得一提的是后者,采用java web start技术来实现程序的本地启动,尤其是对SWT的应用程序,比Applet来实现要更块。 
  
  
  
  
  
  
  
  
  
   
 
  |