紫竹blog

生命应该浪费在美好的事情上……

Struts2文件上传

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>





阅读更多
个人分类: javaEE
想对作者说点什么? 我来说一句

struts2文件上传

2010年09月25日 36KB 下载

文件上传Struts2

2009年04月07日 38KB 下载

struts2文件上传和下载

2010年05月19日 25KB 下载

Struts2文件上传源码

2010年10月20日 3.47MB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭