在struts1.1框架下,利用smartupload实现文件的上传(可以是多个文件)

本文介绍了一个使用Struts框架结合SmartUpload组件实现文件上传的例子。具体包括了前端页面upload.jsp的设计、Struts配置文件的设置、后端处理Action的编写等关键步骤。

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

 
1、前端页面upload.jsp,后台处理程序UplodAction.java
2、struts.config的配置参数如下(没有设置 ActionForm):
<action input="/upload.jsp"  path="/save" scope="request" type="yhp.test.web.UploadAction" validate="false">
      <forward name="success" path="/list.do" />
    </action>
3、upload.jsp页面中主要部分代码
<%@ page contentType="text/html; charset=GBK"%>
<%@ taglib uri=" http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ taglib uri=" http://java.sun.com/jstl/core" prefix="c" %>
<html:html>
<head>
<title> 测试Struts利用SmartUpload上传文件 </title>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
</head>
<body>
<html:form action="/save.do" styleId="formItem" method="post"  enctype="multipart/form-data">
<html:hidden property="id"/>
<html:file property="serverpath" styleClass="input-text"></html:file>
</html:form>
</body>
</html:html>
4、UploadAction.java
import com.jspsmart.upload.File;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.SmartUpload;

public ActionForward doBatchsave(ActionMapping actionMapping,
   ActionForm actionForm, HttpServletRequest httpServletRequest,
   HttpServletResponse httpServletResponse) throws Exception {
SmartUpload mySmartUpload = new SmartUpload();
  mySmartUpload.initialize(getServlet().getServletConfig(),httpServletRequest, httpServletResponse);  
  mySmartUpload.upload();
  //获取除文件以外的相关信息,例如upload.jsp中隐藏控件id的值
  String strId=(String)mySmartUpload.getRequest().getParameter("id");
  Files files=mySmartUpload.getFiles();
  Collection col=files.getCollection();
  Iterator it=col.iterator();
  while(it.hasNext()){
    File file=(File)it.next();    
    String oldFileName=file.getFileName();     
    String extname=file.getFileExt();
    String fileName=Sequence.getSequence()+"."+extname;//产生一个唯一的文件名
    file.saveAs("c://temp/"+fileName);    
  }
  return (actionMapping.findForward("success"));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值