struts文件上传

1.配置web.xml(上一篇有)

 

2.配置struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts>
    	<constant name="struts.i18n.encoding" value="UTF-8"></constant>
		<constant name="struts.configuration.xml.reload" value="true" />
		<constant name="struts.devMode" value="false" />
   		<include file="com/struts/config/file.xml"></include>
    </struts>

 3. FileAction  类

package com.file.action;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

import com.opensymphony.xwork2.ActionSupport;

public class FileAction extends ActionSupport{

	private File uploadFile;
	private String uploadFileContentType;
	private String uploadFileFileName;
	
	public String file(){
		System.out.println("11111111111111111111");
		String realPath = "e:\\现计/piture";
		File file  = new File(realPath);
		if(!file.exists()){
			file.mkdirs();
		}
		try {
			FileUtils.copyFile(uploadFile, new File(file, uploadFileFileName));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace(); 
			return "input";
		}
		return "success";
	}

	/**********get 和 set方法************/
	public File getUploadFile() {
		return uploadFile;
	}

	public void setUploadFile(File uploadFile) {
		this.uploadFile = uploadFile;
	}

	public String getUploadFileContentType() {
		return uploadFileContentType;
	}

	public void setUploadFileContentType(String uploadFileContentType) {
		this.uploadFileContentType = uploadFileContentType;
	}

	public String getUploadFileFileName() {
		return uploadFileFileName;
	}

	public void setUploadFileFileName(String uploadFileFileName) {
		this.uploadFileFileName = uploadFileFileName;
	}
}

 4.配置 file.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts>
    	<constant name="struts-multipart.maxSize" value="16777216"></constant>

    	<package name="testo" extends="struts-default">
    		<action name="f_*" class="com.file.action.FileAction" method="{1}">
    			<result   name="success">/success.jsp</result>
    			<result name="input">/default.jsp</result>
    		</action>
    	</package>
    </struts>

 5.jsp页面访问

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!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=utf-8">
<title>文件上传</title>
</head>
<body>
文件上传
<form action="f_file.action" method="post" enctype="multipart/form-data">
	<input type="file" name="uploadFile">
	<input  type="submit" value="上传">
</form>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值