002——struts的文件上传功能

界面:

<span style="font-size:18px;">  <body>
	<h2>使用Struts上传文件的功能</h2>
	<form action="struts_fileupload.do" method="post" enctype="multipart/form-data">
		<table>
			<tr>
				<td>请选择要上传的文件</td>
				<td><input type="file" name="filePath" size="50"></td>
			</tr>
			<tr>
				<td colspan="2" align="center"><input type="submit" value="上传"></td>		
			</tr>
		</table>
	</form>
  </body></span>

配置文件:

<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>
	<form-beans>
		<form-bean name="fileUploadForm" type="com.java.struts.actionForm.FileUploadActionForm" />
	</form-beans>
  <global-exceptions />
  <global-forwards />
	<action-mappings>
		<action path="/struts_fileupload"
				name="fileUploadForm"
				type="com.java.struts.action.FileUploadAction"
				scope="request">
			<forward name="success" path="/success.jsp"></forward>
		</action>
	</action-mappings>
  <message-resources parameter="ApplicationResources" />
</struts-config>

</span>
逻辑处理类:

<span style="font-size:18px;">package com.java.struts.action;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.java.struts.actionForm.FileUploadActionForm;

public class FileUploadAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		FileUploadActionForm myForm = (FileUploadActionForm) form;
		FormFile fileObj = myForm.getFilePath();
		//得到存放上传文件的目录的真实路径
		String dir = this.getServlet().getServletContext().getRealPath("/upload");
		//如果没有选择文件
		if(fileObj==null) {
			return mapping.findForward("success");
		}
		//获得上传的文件名
		String fileName = fileObj.getFileName();
		//获得上传的文件大小
		int size = fileObj.getFileSize();
		if(size > 1024 * 1024) {
			return mapping.findForward("success");
		}
		//从上传文件中得到一个输入流...之后就可以进行相应操作了
		InputStream is = fileObj.getInputStream();
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dir + "/" + fileName));
		byte[] buffer = new byte[20480];
		int count = 0;
		while((count = is.read(buffer)) != -1) {
			bos.write(buffer, 0, count);
		}
		bos.flush();
		bos.close();
		is.close();
		return mapping.findForward("success");
	}
}
</span>

actionform:

<span style="font-size:18px;">package com.java.struts.actionForm;

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

public class FileUploadActionForm extends ActionForm {

	//文件上传输入域在ActionForm中必须定义为FormFile类型
	private FormFile filePath;

	public FormFile getFilePath() {
		return filePath;
	}

	public void setFilePath(FormFile filePath) {
		this.filePath = filePath;
	}
}</span>



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值