您的位置:首页 > 编程语言 > Java开发

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

2012-03-01 09:54 911 查看
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="增加" onclick="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运行后效果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: