使用Struts2進行上傳下載

文件上傳的必要條件:
1: form表單的 method必須是post
2: form表單的 enctype必須是multipart/form-data
3: 提供 input type=”file”類型上傳輸入域

一:單文件上傳

jsp

<s:fielderror/>
<s:actionerror/>
<form action="${pageContext.request.contextPath}/upload.action" method="post" enctype="multipart/form-data">
    姓名:<input type="text" name="username">
    頭像:<input type="file" name="photo">
    <input type="submit" value="上傳">
</form>

Action

public class SingleUploadAction extends ActionSupport {

    private String username;
    //必須是File類型,名字對應表單上的name
    private File photo;
    //上傳文件名字固定寫法:xxxFileName
    private String photoFileName;
    //文件類型:xxxContentType 上傳文件的MIEM類型
    private String photoContentType;


    public String upload() throws IOException {

        System.out.println(username);
        System.out.println(photoContentType);

        //找到存放上傳文件所在位置的真正路徑
        ServletContext servletContext = ServletActionContext.getServletContext();
        String directory = servletContext.getRealPath("/file");

        //構建目標文件
        File target = new File(directory, photoFileName);

        //寫入文件
        FileUtils.copyFile(photo, target);

        return SUCCESS;

    }

    public File getPhoto() {
        return photo;
    }

    public void setPhoto(File photo) {
        this.photo = photo;
    }

    public String getPhotoContentType() {
        return photoContentType;
    }

    public void setPhotoContentType(String photoContentType) {
        this.photoContentType = photoContentType;
    }

    public String getPhotoFileName() {
        return photoFileName;
    }

    public void setPhotoFileName(String photoFileName) {
        this.photoFileName = photoFileName;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }


}

struts.xml

<!--修改文件上傳大小  10M -->
    <constant name="struts.multipart.maxSize" value="10485760"/>

<package name="upload" extends="struts-default">
        <action name="upload" class="com.lanou.struts.fileupload.SingleUploadAction" method="upload">

            <!--限制文件上傳的類型只能是jpg和png-->
            <interceptor-ref name="defaultStack">
            <param name="fileUpload.allowedExtensions">jpg,png</param>
            </interceptor-ref>

            <result name="success" >/success.jsp</result>
            <result name="input">/singleFileUpload.jsp</result>
        </action>
</package>

二:下載

action

 //定義一個輸入流,名字隨意,in是不能用的
 private InputStream inputStream;
 private String fileName;


    public String download() throws IOException {

        //實現下載:給 inputStream 賦值
        String realPath = ServletActionContext.getServletContext().getRealPath("/噴霧.gif");
        //獲取文件的名字
        fileName = filenameEncoding(FilenameUtils.getName(realPath),
                ServletActionContext.getRequest(),ServletActionContext.getResponse());

        inputStream = new FileInputStream(realPath);


        return SUCCESS;

    }

    //文件名中文適配
    public String filenameEncoding(String filename, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String agent = request.getHeader("User-Agent"); //獲取瀏覽器的類型    System.out.println(agent);
        if (agent.contains("Firefox")) {
            BASE64Encoder base64Encoder = new BASE64Encoder();
            filename = "=?utf-8?B?"                + base64Encoder.encode(filename.getBytes("utf-8"))
                    + "?=";
        } else if(agent.contains("MSIE")) {
            filename = URLEncoder.encode(filename, "utf-8");
        } else if (agent.contains("Safari")){
            filename = new String(filename.getBytes("UTF-8"),"ISO8859-1");
        }else {
            filename = URLEncoder.encode(filename, "utf-8");
        }
        return filename;
    }

struts.xml

<package name="download" extends="struts-default">
        <action name="download" class="com.lanou.struts.down.DownLoadAction" method="download">
            <result name="success" type="stream">
                <!--指定動作類中的輸入流,屬性名-->
                <param name="inputName">inputStream</param>
                <!--通知瀏覽器,以下載的形式打開-->
                <param name="contentDisposition">attachment;filename=${fileName}</param>
            </result>
        </action>

    </package>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章