struts2文件上传(一)

一   上传页面

<form action="${ctx}/upload/upload!upFile.action" method="post" enctype="multipart/form-data">
<tr>
<td>文件1:<s:file name="file"></s:file></td>
</tr>
<tr>
<td>文件2:<s:file name="file"></s:file></td>
</tr>
<tr>
<td align="left"><s:submit name="submit" value="上传"></s:submit></td>
</tr>
</form>

二  action编写

package com.cyb.action;

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

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.xwork2.ActionSupport;

@Namespace("/upload")
@Results( { @Result(name = "upload", location = "/eps/upload/upload.jsp"),
@Result(name = "showResult", location = "/eps/upload/result.jsp") })
public class UploadAction extends ActionSupport {
 /**
  *
  */
 private static final long serialVersionUID = 1L;
 // 上传文件存放路径
 private final static String UPLOADDIR = "/upload";
 // 上传文件集合
 private List<File> file;//前台有对应name为file的文件输入框
 // 上传文件名集合
 private List<String> fileFileName;
 // 上传文件内容类型集合
 private List<String> fileContentType;//名字不能变
 private String fileName;

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

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

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

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

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

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

 public String upFile() throws Exception {
  for (int i = 0; i < file.size(); i++) {
   // 循环上传每个文件
   uploadFile(i);
  }
  System.out.println(this.fileContentType + "###" + this.fileFileName);
  return "showResult";
 }

 // 执行上传功能
 private void uploadFile(int i) throws FileNotFoundException, IOException {
  try {
   InputStream in = new FileInputStream(file.get(i));
   String dir = ServletActionContext.getRequest().getRealPath(UPLOADDIR);//获取上传路径
   File file = new File(dir);
   if (!file.exists()) {
    file.createNewFile();
    file.mkdir();//文件不存在则创建目录
   }
   System.out.println("dir:" + dir);
   File uploadFile = new File(dir, this.getFileFileName().get(i));//创建一个文件,参数1为绝对路径的目录,后者为文件名
   OutputStream out = new FileOutputStream(uploadFile);//将文件写到服务器上指定的目录
   byte[] buffer = new byte[1024 * 1024];
   int length;
   while ((length = in.read(buffer)) > 0) {
    out.write(buffer, 0, length);
   }
   in.close();
   out.close();
  } catch (FileNotFoundException ex) {
   ex.printStackTrace();
  } catch (IOException ex) {
   ex.printStackTrace();
  }
 }
 

 // 如果下载文件名为中文,进行字符编码转换
 public String getDownloadChineseFileName() {
  String downloadChineseFileName = fileName;
  try {
   downloadChineseFileName = new String(downloadChineseFileName .getBytes(), "utf-8");//将文件名编码,防止乱码
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  }
  return downloadChineseFileName;
 }

 public String getFileName() {
  return fileName;
 }

 public void setFileName(String fileName) {
  this.fileName = fileName;
 }
}
dir:E:\workspace\apache-tomcat-6.0.35\webapps\eap\upload
[text/plain]###[731sql日志.sql]

参考文档 http://blog.csdn.net/sxwyf248/article/details/7799785

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值