Struts2文件上传

package com.zhao.action;

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

import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * @author: zhao
 * @time: 2016年5月14日
 * 
 * @description:strut2文件上传
 */
public class FileUpAction extends ActionSupport {
    private static final long serialVersionUID = 1L;
    private File upload;// 上传文件
    private String uploadFileName;// 文件名称
    private String uploadContentType;// 文件类型
    static final String Path = "E://OA/";

    @Override
    public String execute() throws Exception {
        InputStream inputStream = new FileInputStream(upload);
        File target = new File(Path + uploadFileName);
        OutputStream outputStream = new FileOutputStream(target);
        System.out.println(upload.getName() + ":" + upload.getPath());
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
        int c;
        while ((c = bufferedInputStream.read()) != -1) {
            bufferedOutputStream.write(c);
            bufferedOutputStream.flush();// 清空缓存
        }

        bufferedInputStream.close();
        bufferedOutputStream.close();
        return SUCCESS;
    }

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    
    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public String getUploadContentType() {
        return uploadContentType;
    }

    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }

}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
  <constant name="struts.devMode" value="true" />
    
    <package name="file" namespace="/" extends="struts-default">
        <action name="up" class="com.zhao.action.FileUpAction">
            <result>down.jsp</result>
        </action>
    </package>

</struts>
<%@ 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="up.action" method="post" enctype="multipart/form-data">
        <input type="file" name="upload"/>
        <input type="submit" value="Submit"/>
    </form>
</body>
</html>

Action struts.xml 以及前段jsp都有了,功能也实现了。但是有几个需要注意的点:

1:

private File XXX;// 上传文件
private String XXXFileName;// 文件名称
private String XXXContentType;// 文件类型

这三个内容要一致,至于XXX是什么值,是前端jsp中type 为file的那个组件的name

<input type="file" name="upload"/>

如果不一致,必然出错。

我做测试的时候把XXXFileName写成了XXXFilename,然后就无法获取正确的文件名。上传后的文件名就是null。

2:

这个报错信息是说没有找到临时路径,按道理来说是不应该出这个错误的,因为我们有默认的临时路径,既然出错了,那就解决一下:

在strtus.xml或者是struts.properties中配置struts.multipart.saveDir的值,比如说改成/tmp/。就可以了。

posted @ 2016-05-14 16:13  假寐的我  阅读(158)  评论(0编辑  收藏  举报