码迷,mamicode.com
首页 > Web开发 > 详细

struts2-上传文件

时间:2016-11-27 16:39:33      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:name   public   leo   www   output   lan   this   图片   add   

package web;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UploadDemoAction extends ActionSupport {
    private String username;
    private File myfile;
    private String myfileContentType;
    private String myfileFileName;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public File getMyfile() {
        return myfile;
    }
    public void setMyfile(File myfile) {
        this.myfile = myfile;
    }
    public String getMyfileContentType() {
        return myfileContentType;
    }
    public void setMyfileContentType(String myfileContentType) {
        this.myfileContentType = myfileContentType;
    }
    public String getMyfileFileName() {
        return myfileFileName;
    }
    public void setMyfileFileName(String myfileFileName) {
        this.myfileFileName = myfileFileName;
    }
    public String upload(){
        try {
            String path = ServletActionContext.getServletContext().getRealPath(
                    "/upload/file");
            System.out.println("path========"+path);
            InputStream input = new FileInputStream(myfile);
            System.out.println("myfileFileName===="+myfileFileName);
            String allpath=path+"\\"+myfileFileName;
            System.out.println("allpath===="+allpath);
            OutputStream output = new FileOutputStream(allpath);
            IOUtils.copy(input, output);
            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return INPUT;
        }
    }
}

 

<%@ 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>Insert title here</title>
</head>
<body>
<form action="upload.action" method="post" enctype="multipart/form-data">
    用户名:<input type="text" name="username"/>
    文件:<input type="file" name="myfile" />
    <input type="submit" value="提交"/>
</form>
</body>
</html>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <!-- 忽略struts格式 -->
    <constant name="struts.ui.theme" value="simple"></constant>
    <constant name="struts.configuration.xml.reload" value="true"></constant>
    <package name="houseuser" extends="struts-default" namespace="/">
    
    
    
    <!-- 上传图片示例-->
        <action name="upload" class="web.UploadDemoAction" method="upload">
            <result >success.jsp</result>
        </action>
    </package>
    
</struts>    

 

struts2-上传文件

标签:name   public   leo   www   output   lan   this   图片   add   

原文地址:http://www.cnblogs.com/sincoolvip/p/6106515.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!