webwork文件上传

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
1.webwork.properties相关配置如下:

代码

webwork.locale=zh_CN
webwork.multipart.parser=jakarta
webwork.i18n.encoding = GBK
webwork.multipart.saveDir=d:/yourtempdir
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
2.action如下:


代码

pimport java.io.*;

import com.opensymphony.xwork.ActionSupport;
public class Filetestaction extends ActionSupport {
protected Log log = LogFactory.getLog(this.getClass());
File file;
String fileContentType;
String fileFileName;
public String execute() throws Exception {
   if (null != file) {
   FileOutputStream outputStream = new FileOutputStream("d:/temp/"
     + fileFileName);
   FileInputStream fileIn = new FileInputStream(file);
   byte[] buffer = new byte[1024];
   int len;
   while ((len = fileIn.read(buffer)) > 0) {
    outputStream.write(buffer, 0, len);
   }
   fileIn.close();
   outputStream.close();
  }
  return SUCCESS;
}
public File getFile() {
  return file;
}
public void setFile(File file) {
  this.file = file;
}
public String getFileContentType() {
  return fileContentType;
}
public void setFileContentType(String fileContentType) {
  this.fileContentType = fileContentType;
}
public String getFileFileName() {
  return fileFileName;
}
public void setFileFileName(String fileFileName) {
  this.fileFileName = fileFileName;
}
}

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
3页面如下:


代码

<%@ page contentType="text/html; CHARSET=utf8" %>
<%@ taglib uri="webwork" prefix="ww" %>
<html>
<head>
<title>My JSF 'fileupload.jsp' starting page</title>
</head>
 
<body>

<ww:form name="'test'" method="'post'" enctype="'multipart/form-data'" >
  <ww:textarea name="'name'" label="'Name'" />
    <ww:file label="'File'" name="'file'"  />
    <ww:submit value="'Upload'"/>
</ww:form>

filename: <ww:property value="fileFileName"/><br>
name: <ww:property value="name"/>
</body>
</html>


::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
4.xwork.xml配置如下:



代码

     <action name="fileupload" class="test.FileUploadAction">
     <interceptor-ref name="fileUploadStack"></interceptor-ref>
     <result name="success" type="dispatcher">
<param name="location">/fileupload.jsp</param>
</result>    
     </action>

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

5.包
  为了使用文件上传的特性,必须为所选择的多部份解析器(Multipartparser)包含下确的JAR文件,对于jakarta,相应的jar文件文件则是commons-fileupload.jar

::::::::::::::::::::::::::::::::::
如果需要同时上传多个文件,在html中定义多个同名的input,
在action中定义private File[] myfile;并提供getter和setter则可以取得上传文件数组.

上传多个文件时,需要注意,要采用jakarta,不要采用pell,否则只能取得第一个文件.
采用jakarta的时候,File的filename是不能用的,取到的是upload_xxx.tmp.
在action中定义private String[] myfileFileName;可以取得真实的文件名.

你可能感兴趣的:(html,jsp,xml,JSF,Webwork)