现在的位置: 首页 > 综合 > 正文

Struts2文件上传

2017年04月16日 ⁄ 综合 ⁄ 共 3179字 ⁄ 字号 评论关闭

Struts2文件上传:

上传页面:index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <base href="<%=basePath%>">
    
<title>文件上传测试</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
</head>
  
<body>
<p>文件上传</p>
<form action="fileAction" method="post" enctype="multipart/form-data" name="form1">
  <p>标题:
    <input type="text" name="title" id="title">
</p>
  <p>文件:
    <input type="file" name="process" id="process">
</p>
  <p>
    <input type="submit" name="button" id="button" value="提交">
  </p>
</form>
<p>  <br>
</p>
</body>
</html>

对应后台接收类:

package com.model.dto;

import java.io.File;

public class FileModel {
	private String title = null;
	private File process = null;
	private String uploadContentType = null;
	
	private String savePath = null;
	public String getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public File getProcess() {
		return process;
	}
	public void setProcess(File process) {
		this.process = process;
	}
	public String getSavePath() {
		return savePath;
	}
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	
}	

处理的Action:

package com.action;

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

import org.apache.struts2.ServletActionContext;

import com.model.dto.FileModel;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class FileAction extends ActionSupport implements ModelDriven<FileModel>{
	private FileModel fileModel = new FileModel();
	public String execute(){
		System.out.println(fileModel.getTitle());
		System.out.println(fileModel.getSavePath());
		System.out.println(fileModel.getProcess());
		
		try {
			File f = new File(ServletActionContext.getRequest().getRealPath(fileModel.getSavePath()));
			if(!f.exists()){
				f.mkdirs();
			}
			FileOutputStream fos = new FileOutputStream(f+"/1.txt");
			FileInputStream fis = new FileInputStream(fileModel.getProcess());
			byte [] b = new byte[1024];
			while((fis.read(b))!=-1){
				fos.write(b, 0, b.length);
			}
			fos.close();
			fis.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	public FileModel getModel() {
		return fileModel;
	}
}

Action的配置文件:

<?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.enable.DynamicMethodInvocation" value="true" />
    <constant name="struts.devMode" value="true" />

    <package name="default" namespace="/" extends="struts-default">
        <action name="fileAction" class="com.action.FileAction" >
        <param name="savePath">/upload</param>
            <result>
                /success.jsp
            </result>
        </action>
    </package>

    <!-- Add packages here -->

</struts>

抱歉!评论已关闭.