使用Struts进行文件上传

本文介绍如何使用Struts框架实现文件上传功能,包括上传表单的设计、ActionForm及Action类的具体实现方式,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  使用Struts进行文件上传需要Struts提供的类:org.apche.struts.uploadFormfile 改类在lib目录下的commons-fileuoload.jar文件中:

上传表单代码:
  
<html:form action="/upload" enctype="multipart/form-data">
<html:file property="file">
<input type="submit" value="上传"  name="submit">
<input type="reset" value="重设">
对应的ActionForm代码:
public class FileActionForm extends ActionForm{
       
private Formfile file;
       
   
public void setFile(Formfile file){
            this.file = file;
       }

   
public Formfile getFile(){
            
return file;
       }


}
控制文件上传的Action类:
public class uploadAction extends Action{
   
public ActionForWard execute(ActionMapping mapping,ActionForm form,
         HttpServletRequest request,HttpServletResponse response){
     FileActionForm fileform 
= (FileActionForm)form;
        
     
if(fileform != null){
         Formfile file 
=(Formfile)fileform.getFile();
         
if(file != null){
            String filename 
= file.getFileName();
            
try{
               
byte[] files = file.getFileData();
               
//uploadfile为上传的存储路径,以原文件的名称为上传文件的名称
               FileOutputStream out =
                   
new FileOutputStream(servlet.getServletContext()
                                        .getRealPath(
""/)+
                                        
"/uploadfile/" + filename);
               System.out.println(
"文件开始上传.....");
               out.write(files);
               System.out.println(
"文件上传完毕......");
               out.close();
            }
catch(Exception ex){
                 System.out.println(ex.getMessage());
            }

          }    
      }
     
return mapping.findForword("....");
        
   }   
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值