java struts中实现上传多张图片代码

本文展示了如何在Java Struts框架下实现多张图片上传的功能。通过`textForm`类来处理文件上传,`UploadFile`类保存上传的文件信息,`textAction`处理文件保存并转发到成功页面,而`struts-config.xml`配置了相应的Action映射。在`text.jsp`中,使用JavaScript动态添加文件输入字段,允许用户上传多张图片。
摘要由CSDN通过智能技术生成

 UploadFile.java

import java.io.Serializable;

import org.apache.struts.upload.FormFile;

public class UploadFile implements Serializable{
 private FormFile file;

 public FormFile getFile() {
    return file;
 }

 public void setFile(FormFile file) {
    this.file = file;
 }


}

 

textForm.java

import java.util.ArrayList;
import java.util.List;
import com.fw.web.BaseForm;

public class textForm extends BaseForm {
 private static final long serialVersionUID = 1L;

 private String id;

 private String title;

 private String subTitle;

 private String content;

 private String modelId;

 private List myFiles;


 public List getMyFiles() {
    return myFiles;
 }

 public void setMyFiles(List myFiles) {
    this.myFiles = myFiles;
 }

 public textForm() {
    myFiles = new ArrayList();
 }

 // 留意这个方法的定义 不加中间的轮回是会犯错的

 public UploadFile getUploadFile(int index) {
    int size = myFiles.size();
    if (index > size - 1) {
     for (int i = 0; i < index - size + 1; i++) {
      myFiles.add(new UploadFile());
     }
    }
    return (UploadFile) myFiles.get(index);
 }

 

 public String getModelId() {
    return modelId;
 }

 public void setModelId(String modelId) {
    this.modelId = modelId;
 }


 public String getId() {
    return id;
 }

 public void setId(String id) {
    this.id = id;
 }

 public String getTitle() {
    return title;
 }

 public void setTitle(String title) {
    this.title = title;
 }

 public String getSubTitle() {
    return subTitle;
 }

 public void setSubTitle(String subTitle) {
    this.subTitle = subTitle;
 }

 public String getContent() {
    return content;
 }

 public void setContent(String content) {
    this.content = content;
 }

 @Override
 public void toForm() {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void toPojo() {
  // TODO Auto-generated method stub
  
 }

}

 

textAction.java

import java.util.Date;
import java.util.List;

import org.apache.struts.action.ActionForward;
import org.apache.struts.upload.FormFile;

import com.fw.common.web.ActionContext;
import com.fw.web.CheckBaseAction;
import com.wsjk.web.form.UploadFile;
import com.wsjk.web.form.textForm;

public class textAction extends CheckBaseAction {
 public ActionForward doPicText(ActionContext ac) throws Exception{
   
     textForm newsForm = (textForm)ac.getForm();
  
   
     String Title=newsForm.getTitle();
     String SubTitle=newsForm.getSubTitle();
     String Content=newsForm.getContent();
     Date CreateDate=new Date();
   
  
   
     //保留图片
     String bathPath = ac.getRequest().getSession().getServletContext().getRealPath("/") + "news";
     //System.out.println(bathPath);
     StringBuffer sb = new StringBuffer();
     List myFiles = newsForm.getMyFiles();
   
     for (int i = 0; i < myFiles.size(); i++) {
      UploadFile uploadFile = (UploadFile) myFiles.get(i);
           FormFile file = uploadFile.getFile();
           String fileType = file.getFileName().substring(file.getFileName().lastIndexOf(".") + 1, file.getFileName().length());
      String fileName = FileUtil.getRandomFileName() + "." + fileType;
      if(file.getFileSize() > 0){
       FileUtil.saveFile(file.getInputStream(), bathPath , fileName);
      }
      sb.append("/news/" + fileName + ";");
     }  
   
     String PicturePath=sb.toString();
     return forward(ac,"success");
  }
 public ActionForward dojumpPicText(ActionContext ac) throws Exception{
  return forward(ac,"success");
 }
}

 

struts-config.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
 <!-- ========== Form Bean Definitions =================================== -->
 <form-beans>
  <form-bean name="textForm" type="com.wsjk.web.form.textForm"></form-bean>
 </form-beans>
 <!-- ========== Global Forward Definitions ============================== -->


 <!-- ========== Action Mapping Definitions ============================== -->
 <action-mappings>
 <!-- agrinfo begin-->

 <!-- text begin -->
   <action path="/text" parameter="PicText" name="textForm" type="com.wsjk.web.action.textAction">
      <forward name="success" path="/text.jsp"/>
    </action>
       <action path="/textjump" parameter="jumpPicText" name="textForm" type="com.wsjk.web.action.textAction">
      <forward name="success" path="/text.jsp"/>
    </action>
     <!-- text end -->
 </action-mappings>

</struts-config>

text.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">

<script type="text/javascript">
   var t = 1;
   function addFile()
   {
   alert("------------");
    var parent = document.getElementById("more");

    var br = document.createElement("br");
    var input = document.createElement("input");
    var button = document.createElement("input");

    input.type = "file";
    input.name = "uploadFile[" + (t++) + "].file";
    input.size = "30";
    button.type = "button";
    button.value = "删除";

    button.onclick = function()
    {
     parent.removeChild(br);
     parent.removeChild(input);
     parent.removeChild(button);
   
    };

    parent.appendChild(br);
    parent.appendChild(input);
    parent.appendChild(button);
   }
</script>

</head>
<body>
<form action="text.do?method=add" method="post" enctype="multipart/form-data">
   <input type="hidden" name="id" value="${news.id }">
   <table width="90%" border="0" align="left" cellpadding="0" cellspacing="1" class="newTable">
    <tr>
    <td class="newTd">新闻标题</td>
    <td class="newTd"><input type="text" name="title" size="30"></td>
    </tr>
    <tr>
    <td class="newTd">新闻副标题</td>
    <td class="newTd"><input type="text" name="subTitle" size="30"></td>
    </tr>
    <tr>
    <td class="newTd">新闻内容</td>
    <td class="newTd"><textarea class="ckeditor" name="content"></textarea></td>
    </tr>
     <tr>
    <td class="newTd">新闻图片</td>
    <td class="newTd" id="more"><input type="file" name="uploadFile[0].file" size="30"><input type="button" value="增加" οnclick="addFile();"></td>
    </tr>
    <tr>
    <td class="newTd">新闻所属板块</td>
    <td class="newTd">
       <select id="modelSelect" name="modelId">
       </select>
    </td>
    </tr>
    <tr>
    <td align="center" colspan="2">
       <input type="submit" value="保留">    
       <input type="reset" value="重置">
    </td>
    </tr>
   </table>
   </form>

</body>
</html:html>


里面的一些路径还要根据你项目的路径进行一下改动,项目中可能还需要添加jspsmartupload.jar的文件,下载一个即可

运行textjump.do运行后效果如下:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值