struts1 上传文件

java代码:

package com.struts1.form;

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

public class UploadFileForm extends ActionForm {

	private static final long serialVersionUID = 1L;
	
	private FormFile formFile;

	public FormFile getFormFile() {
		return formFile;
	}

	public void setFormFile(FormFile formFile) {
		this.formFile = formFile;
	}
}

成员变量的顺序要和jsp页面上的输入框的顺序一致。


package com.struts1.action;

import java.io.FileOutputStream;

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

import org.apache.commons.io.IOUtils;
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.struts1.form.UploadFileForm;

public class UploadAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		UploadFileForm uploadFileForm = (UploadFileForm)form;
		FormFile formFile = uploadFileForm.getFormFile();
		if (formFile == null) {
			return mapping.findForward("uploadError");
		}
		String formFileName = formFile.getFileName();
		int pos = formFileName.lastIndexOf(".");
		String fileName = "c:\\";
		if (pos != -1) {
			fileName = fileName + formFileName.substring(0, pos) + System.currentTimeMillis() + formFileName.substring(pos);
		} else {
			fileName = fileName + formFileName + System.currentTimeMillis();
		}
		FileOutputStream fos = null;
		
		try {
			fos = new FileOutputStream(fileName);
			IOUtils.copy(formFile.getInputStream(), fos);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeQuietly(formFile.getInputStream());
			IOUtils.closeQuietly(fos);
		}
		
		return mapping.findForward("success");
	}

}



jsp代码:

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-nested" prefix="nested" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Insert title here</title>
</head>
<body>
<html:form action="uploadFile.do" enctype="multipart/form-data" method="post">
	<html:file property="formFile"></html:file><br>
	<html:submit />
</html:form>
</body>
</html>

注意:

enctype="multipart/form-data"

method="post"


struts配置文件:

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!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="uploadFileForm" type="com.struts1.form.UploadFileForm" />
	</form-beans>

	<global-exceptions>
	</global-exceptions>

	<global-forwards>
	</global-forwards>

	<action-mappings>
		<action path="/uploadFile" type="com.struts1.action.UploadAction"
			name="uploadFileForm" scope="request" validate="false" input="/index.jsp">
			<forward name="success" path="/success.jsp" />
			<forward name="uploadError" path="/uploadError.jsp" />
		</action>
	</action-mappings>
	
	<controller maxFileSize="1M" />

</struts-config>

controller用来对上传的文件大小等进行控制,超过最大大小就抛异常。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值