struts1多文件上传

jsp页面内容:

<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
 
<html>
 <head>
  <title>JSP for FileLoadForm form</title>
 </head>
 <body>
  <html:form action="/fileLoad" enctype="multipart/form-data" method="post">
   文件2 : <html:file property="file[1]"/><html:errors property="file[1]"/><br/>
   文件1 : <html:file property="file[0]"/><html:errors property="file[0]"/><br/>
   <html:submit value="点击上传"/><html:cancel value="取消"/>
  </html:form>
 </body>
</html>

 

formBean内容:

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.yourcompany.struts.form;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

public class FileLoadForm extends ActionForm {

 private FormFile[] file;
 //----------------------------------
 private String  filepath;
 public String getFilepath() {
  return filepath;
 }
 public void setFilepath(String filepath) {
  this.filepath = filepath;
 }
 //-----------------------------------
 public FileLoadForm(){
  file = new FormFile[2];
 }
 public ActionErrors validate(ActionMapping mapping,
   HttpServletRequest request) {
  
  return null;
 }

 
 public void reset(ActionMapping mapping, HttpServletRequest request) {
  
 }


 public FormFile getFile(int i) {
  return file[i];
 }


 public void setFile( int i,FormFile file) {
  this.file[i] = file;
  }
 }

 

action的内容:

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.yourcompany.struts.action;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.yourcompany.struts.form.FileLoadForm;
import com.yourcompany.struts.form.LoadfileForm;

public class FileLoadAction extends Action {

 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  
  if (form instanceof FileLoadForm) {
   String encoding = request.getCharacterEncoding();
   if(encoding!=null && encoding.equalsIgnoreCase("utf-8")){
    response.setContentType("text/html;charset=utf-8");
   }
   FileLoadForm fileLoadForm = (FileLoadForm) form;
   for(int i=0;i<2;i++){
    System.out.println(i);
   FormFile loadfile = fileLoadForm.getFile(i);//取得上传文件
   try {
    InputStream stream = loadfile.getInputStream();//把文件读入
    String filePath = this.getServlet().getInitParameter("uploadPath");//取得当前系统路径
    String path = filePath+"/"+loadfile.getFileName();
    request.getSession().setAttribute("filePath", path);
    OutputStream bos = new FileOutputStream(path);//建立一个上传文件输出流
    int bytesRead = 0;
    byte[] buffer = new byte[8192];
    while((bytesRead = stream.read(buffer, 0, 8192))!=-1){
     bos.write(buffer, 0, bytesRead);
    }
    bos.close();
    stream.close();
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
  }
  return mapping.findForward("ok");
 }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值