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

利用Struts上传文件

时间:2015-05-22 13:04:03      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:

在利用struts2完成上传文件到服务器时,遇到获取不到文件名

原因是在Action中的属性名没有和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>My JSP ‘main.jsp‘ starting page</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>
    <h1>上传文件</h1>
   <form action="upload" method="post" enctype="multipart/form-data">
   请选择文件:<input type="file" name="file"/>
   <input type="submit" value="提交"/>
   </form>
  </body>
</html>

Action:

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

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class Upload extends ActionSupport{
    
    private File file;
    private String fileContentType;
    private String fileFileName;
    public File getFile() {
        return file;
    }
    public void setFile(File file) {
        this.file = file;
    }
    public String getFileContentType() {
        return fileContentType;
    }
    public void setFileContentType(String fileContentType) {
        this.fileContentType = fileContentType;
    }
    
    
    public String getFileFileName() {
        return fileFileName;
    }
    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }
    public String upload(){
        System.out.println(this.fileContentType);
        System.out.println(this.fileFileName);
        
        InputStream is=null;
        try {
            is=new FileInputStream(file);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        
        String path=ServletActionContext.getServletContext().getRealPath("/");
        System.out.println(path);
        
        File toFile=new File(path, this.getFileFileName());
        
        OutputStream os=null;
        try {
            os=new FileOutputStream(toFile);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        
        byte[] buffer=new byte[1024];
        int length=0;
        try {
            while((length=is.read())>0){
                os.write(buffer, 0, length);
            }
            is.close();
            os.close();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return SUCCESS;
    }

}

 

Action中该这样配置

 

private File file;
 private String fileContentType;
 private String fileFileName;

 

ContentType和FileName是固定的,前缀为<input>中的name的值

利用Struts上传文件

标签:

原文地址:http://www.cnblogs.com/zyxiaohuihui/p/4521998.html

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