struts2实现文件上传

本文介绍了一个基于Struts2框架实现文件上传的例子。通过定义FileUploadAction类处理文件上传逻辑,利用struts.xml配置文件指定上传路径及展示页面。演示了从文件选择到上传成功的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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. 结果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值