struts2实现文件上传

compiler 同时被 2 个专栏收录
2 篇文章 0 订阅
1 篇文章 0 订阅

struts 2 实现文件上传

1.结构

2. FileUploadAction.java

package com.hainu.action;

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


import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport{
private File upload;
private String uploadFileName;//XXXFileName is used to get the name of the file
private String uploadContentType;// XXXContentType is used to get the type of the file
//private String name;


public String execute() throws Exception{
    String path = ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload");  
    String filename = path+File.separator+uploadFileName; 
    System.out.println(uploadFileName);
    System.out.println(filename);
    FileInputStream in = new FileInputStream(upload);  
    FileOutputStream out = new FileOutputStream(filename);  
    byte[]b = new byte[1024];  
    int len = 0;  
    while((len=in.read(b))>0){  
        out.write(b,0,len);  
    }  
    out.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;
}



}

3. struts.xml

<package name="file" namespace="/fileoperate" extends="struts-default">
<action name="FileUp">
<result>upload.jsp</result>
</action>

<action name="Upload" class="com.hainu.action.FileUploadAction">
<result name="SUCCESS">uploadsuccess.jsp</result>
</action>
</package>

4. upload.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
 <%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Upload File</title>
</head>
<body>
<s:form action="Upload" method="post" enctype="multipart/form-data">
    <!-- 此处必须为multipart/form-data,而且必须使用post方法 -->  
    <s:file name="upload" label="UploadFile"/>
    <s:submit value="submit"/>
</s:form>  
</body>
</html>

5. uploadsuccess.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
success
</body>
</html>

6. 结果

 

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:博客之星2019 设计师:CSDN官方博客 返回首页

打赏作者

khuangliang

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值