Struts 2上传文件小谈 (转)

下面谈谈Struts 2框架的文件上传应用
(1)原理:Struts 2框架没有提供文件上传的解析器(也可以说没有提供自己的文件上传组件),它是借助于其他文件上传组件。例如,Struts 2默认的使用Jakarta的commons-fileupload.jar和commons-io.jar。但Struts 2在原有的上传解析器中做了更进一步的封装,更进一步简化上传文件。
(2)首先,在struts.properties文件中配置Struts 2上传文件的解析器
#默认的使用Jakarta的commons-fileupload文件解析器
struts.multipart.parser = jakarta
(3)在WEB-INF/lib中加入两个包,commons-fileupload.jar和commons-io.jar。
(4)编写实现文件上传的Action类,代码如下:
package com.gxa.edu.struts2.ch4.action;
/**
* 利用Struts 2实现文件上传功能
* @author Administrator
*
*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import org.apache.struts2.ServletActionContext;
public class FileUploadAction {

private File fileUpload;//获取上传文件
private String fileUploadContentType;//获取上传文件的类型,注意上传类型变量命名方式
private String fileUploadFileName;//获取上传文件的名称
private String fileSavePath;//设置上传文件的保存路径,利用struts2框架的设置注入。在struts.xml文件配置<param name = "fileSavePath"/>关键字
public String getFileSavePath() {
return ServletActionContext.getServletContext().getRealPath(fileSavePath);
}
public void setFileSavePath(String fileSavePath) {
this.fileSavePath = fileSavePath;
}
public File getFileUpload() {
return fileUpload;
}
public void setFileUpload(File fileUpload) {
this.fileUpload = fileUpload;
}
public String getFileUploadContentType() {
return fileUploadContentType;
}
public void setFileUploadContentType(String fileUploadContentType) {
this.fileUploadContentType = fileUploadContentType;
}
public String getFileUploadFileName() {
return fileUploadFileName;
}
public void setFileUploadFileName(String fileUploadFileName) {
this.fileUploadFileName = fileUploadFileName;
}

public String upload() throws Exception {
System.out.println("===利用struts2上传文件===");
System.out.println("文件类型===" + this.fileUploadContentType);
FileInputStream fis = new FileInputStream(getFileUpload());
File file = new File(getFileSavePath().replaceAll("[url=]\\\\[/url]", "/"));
if (!file.exists()) {
file.mkdirs();
}
FileOutputStream fos = new FileOutputStream(getFileSavePath().replaceAll("[url=]\\\\[/url]", "/") + "/" + getFileUploadFileName());
int i = 0;
byte[] buf = new byte[1024];
while ((i = fis.read(buf)) != -1) {
fos.write(buf, 0, i);
}
return "success";
}
}
(5)编写一个upload.jsp文件,代码如下:
<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib prefix = "s" uri = "/struts-tags" %>
<%
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 'upload.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>
This is the FileUpload of Struts 2 page. <br>
<s:form. action = "upload.action" enctype = "multipart/form-data">
<input type = "file" name = "fileUpload"></input>
<input type = "submit" value = "提交"></input>
</s:form>
</body>
</html>
(6)配置struts-fileupload.xml文件,代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<!-- 如果要上传中文名称的文件,将"struts.i18n.encoding"的值设置为GBK -->
<constant name="struts.i18n.encoding" value="GBK"></constant>
<package name="fileupload" extends = "struts-default" namespace = "/ch4">
<action name="upload" class = "com.gxa.edu.struts2.ch4.action.FileUploadAction" method = "upload">
<param name="fileSavePath">/upload</param>
<result>/ch4/upload.jsp</result>
<result name = "input">/ch4/upload.jsp</result>
</action>
</package>
</struts>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值