posts - 325,  comments - 25,  trackbacks - 0

一.单文件上传
添加jar包
commons-fileupload-1.2.1.jar
commons-io-1.4.jar
在webroot创建一个文件如upload用于保存上传的文件
1.upload.jsp
 <body>
   <s:form action="upload" enctype="multipart/form-data">
   <s:file name="file" label="file"></s:file>
   <s:submit label="upload"></s:submit>
  </s:form>
 </body>

2.UploadAction.java

package com.test.upload;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {
 private File file;
 private String fileFileName;
 private String fileContentType;
 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;
 }
 @Override
 public String execute() throws Exception {
  //1.构建一个输入流
  InputStream is=new FileInputStream(file);
  //2.构建一个上传文件路径
  String root=ServletActionContext.getRequest().getRealPath("/upload");
  //3.获得一个本地文件
  File diskFile=new File(root,this.getFileFileName());
  //4.构建输出流
  OutputStream os=new FileOutputStream(diskFile);
  //5.能过字节写入输出流
  byte[] buffer=new byte[400];
  int length=0;
  while((length=is.read(buffer))>0)
  {
   os.write(buffer,0,length);
  }
  is.close();
  os.close();
  return SUCCESS;
 }
}

3.配置struts.xml
<constant name="struts.multipart.saveDir" value="c:\"></constant>
 <package name="FileuploadTest" extends="struts-default">
  <action name="upload" class="com.test.upload.UploadAction">
   <result name="success">/uploadSuccess.jsp</result>
  </action>
 </package>

二.多文件上传
1.upload.jsp页面
<body>
  <s:form action="upload" enctype="multipart/form-data">
   <s:file name="file" label="file"></s:file>
   <s:file name="file" label="file1"></s:file>
   <s:file name="file" label="file2"></s:file>
   <s:submit label="submit"></s:submit>
  </s:form>
  </body>

2.UploadAction.java

package com.test.upload;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {
 private List<File> file;

 private List<String> fileFileName;

 private List<String> fileContentType;

 public List<File> getFile() {
  return file;
 }

 public void setFile(List<File> file) {
  this.file = file;
 }

 public List<String> getFileContentType() {
  return fileContentType;
 }

 public void setFileContentType(List<String> fileContentType) {
  this.fileContentType = fileContentType;
 }

 public List<String> getFileFileName() {
  return fileFileName;
 }

 public void setFileFileName(List<String> fileFileName) {
  this.fileFileName = fileFileName;
 }

 @Override
 public String execute() throws Exception {
  for (int i = 0; i < file.size(); i++) {
   // 1.构建一个输入流
   InputStream is = new FileInputStream(file.get(i));
   // 2.构建一个上传文件路径
   String root = ServletActionContext.getRequest().getRealPath(
     "/upload");
   // 3.获得一个本地文件
   File diskFile = new File(root, this.getFileFileName().get(i));
   // 4.构建输出流
   OutputStream os = new FileOutputStream(diskFile);
   // 5.能过字节写入输出流
   byte[] buffer = new byte[400];
   int length = 0;
   while ((length = is.read(buffer)) > 0) {
    os.write(buffer, 0, length);
   }
   is.close();
   os.close();
  }
  return SUCCESS;
 }
}
3.struts.xml
同上

posted on 2008-05-08 11:23 长春语林科技 阅读(818) 评论(0)  编辑  收藏 所属分类: struts2

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


网站导航:
 
<2008年5月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

 

长春语林科技欢迎您!

常用链接

留言簿(6)

随笔分类

随笔档案

文章分类

文章档案

相册

收藏夹

搜索

  •  

最新评论

阅读排行榜

评论排行榜