JSP/Java/Struts实现文件上传

 首先说明struts.xml里面如果要限制上传文件的类型可以配置一个拦截器:

<!-- 通过动态设置allowTypes的属性来动态指定允许上传的文件类型 -->

             <param name="allowTypes">image/bmp,image/png,image/gif,image/jpeg</param>

单文件上传JSP:

<body>
		<center>
			<form action="upload.action" method="post"
				enctype="multipart/form-data">
				<table cellpadding="0" cellspacing="0" style="width: 350px;">
					<tr>
						<td colspan="2" class="getcenter">
							单文件上传
						</td>
					</tr>
					<tr>
						<td>
							文件标题:
						</td>
						<td>
							<input type="text" name="title" class="txt" />
						</td>
					</tr>
					<tr>
						<td>
							选择文件:
						</td>
						<td>
							<input type="file" name="file" class="file" />
						</td>
					</tr>
					<tr>
						<td colspan="2" class="getcenter">
							<input type="submit" value="上传" />
							<input type="reset" value="重置" />
						</td>
					</tr>
				</table>
			</form>
		</center>
	</body>
单文件上传成功界面:
<body>
		文件上传成功!!!
		<hr>
		文件标题:${upload.title}
		<br />
		文件名称:${upload.fileFileName}
		<br />
		文件类型:${upload.fileContentType}
		<br />
	</body>

多文件:

<script type="text/javascript">
      function addTR(){
         var tab = document.getElementById("tab");
         var newRow = tab.insertRow(tab.rows.length);
         newRow.insertCell(0).innerHTML="<input type=\"file\" name=\"file\"/>";
      }
      
      function delRow(){
         var tab = document.getElementById("tab");
         if(tab.rows.length>3){
            tab.deleteRow(tab.rows.length-1);
         }else{
            alert("至少保留二行!");
         }
      }
         
  </script>
	</head>

	<body>
		<center>
			<form action="multiupload.action" method="post"
				enctype="multipart/form-data">
				<table cellpadding="0" cellspacing="0" style="width: 500px;"
					id="tab">
					<tr>
						<td>
							<input type="button" value="增加上传文件" οnclick="addTR()" />
							<input type="button" value="减少" οnclick="delRow()" />
							<input type="reset" value="重置" />
							<input type="submit" value="上传" />
						</td>
					</tr>
					<tr>
						<td>
							<input type="file" name="file" />
						</td>
					</tr>
					<tr>
						<td>
							<input type="file" name="file" />
						</td>
					</tr>
				</table>
			</form>
		</center>
	</body>

多文件成功界面:

<body>
		<c:forEach items="${fileFileName}" var="file" varStatus="status">
		文件上传成功!!!
		<hr>
		
		<br />
		文件名称:${upload.fileFileName[status.index]}
		<br />
		文件类型:${upload.fileContentType[status.index]}
		<br />
		</c:forEach>
	</body>

web.xml://配置struts-clean,避免第一次上传取不到文件

<filter>
		<filter-name>struts-cleanup</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts-cleanup</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

struts.xml

<struts>
	<constant name="struts.i18n.encoding" value="utf-8"></constant>
	<package name="default" namespace="/" extends="struts-default">
		<action name="product" class="com.tudou.struts.action.ProductAction">
		</action>
		<action name="upload" class="com.tudou.struts.action.FileUpLoadAction">
			<param name="savePath">/uploadfiles</param>
			<result name="success">/fileSuccess.jsp</result>
		</action>
		<action name="multiupload" class="com.tudou.struts.action.MultiFileUpLoadAction">
			<param name="savePath">/uploadfiles</param>
			<result name="success">/success.jsp</result>
		</action>
	</package>
	<package name="myPackage" namespace="/" extends="struts-default">
		<action name="login" class="com.tudou.struts.action.LoginAction">
			<result name="input">/login.jsp</result>
			<result name="index">/index.jsp</result>
		</action>
	</package>
单文件action:
package com.tudou.struts.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

public class FileUpLoadAction {

	private String title; // 文件标题
	private File file; // 文件域
	private String fileContentType; // 文件类型
	private String fileFileName; // 文件名
	private String savePath; // 上传地址

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public String getTitle() {
		return title;
	}

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

	public File getFile() {
		return file;
	}

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

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	// 返回保存地址
	public String getSavePath() throws Exception {
		return ServletActionContext.getRequest().getRealPath(savePath);
	}

	public String execute() throws Exception {
		// 以服务器的文件保存地址的原文件名建立上传文件输出流
		FileOutputStream fos = new FileOutputStream(this.getSavePath() + "\\"
				+ this.getFileFileName());
		// 以上传文件建立文件输入流
		FileInputStream fis = new FileInputStream(this.getFile());
		// 将上传文件内容写入服务器
		byte[] b = new byte[1024];
		int len = 0;
		while ((len = fis.read(b)) > 0) {
			fos.write(b, 0, len);
		}
		ActionContext.getContext().put("upload", this);
		return "success";
	}
}

多文件上传action:

package com.tudou.struts.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

public class MultiFileUpLoadAction {
	// private String[] title; // 文件标题
	private File[] file; // 文件域
	private String fileContentType[]; // 文件类型
	private String[] fileFileName; // 文件名
	private String savePath; // 上传地址

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	// public String[] getTitle() {
	// return title;
	// }
	//
	// public void setTitle(String[] title) {
	// this.title = title;
	// }

	public File[] getFile() {
		return file;
	}

	public void setFile(File[] file) {
		this.file = file;
	}

	public String[] getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String[] fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String[] getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String[] fileFileName) {
		this.fileFileName = fileFileName;
	}

	// 返回保存地址
	public String getSavePath() throws Exception {
		return ServletActionContext.getRequest().getRealPath(savePath);
	}

	public String execute() throws Exception {
		for (int i = 0; i < file.length; i++) {
			// 以服务器的文件保存地址的原文件名建立上传文件输出流
			FileOutputStream fos = new FileOutputStream(this.getSavePath()
					+ "\\" + this.getFileFileName()[i]);
			// 以上传文件建立文件输入流
			FileInputStream fis = new FileInputStream(this.getFile()[i]);
			// 将上传文件内容写入服务器
			byte[] b = new byte[1024];
			int len = 0;
			while ((len = fis.read(b)) > 0) {
				fos.write(b, 0, len);
			}
		}
		ActionContext.getContext().put("upload", this);
		return "success";
	}
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值