struts2文件上传功能的实现

写这个功能是为了弥补KindEditor在线编辑器的不足,KindEditor无法上传附件,其他功能还可以。


除必要的包外,需要写的有如下几个文件:upload.jsp ------>struts.xml------>UploadAction.java------->uploadlist.jsp

下面是完整代码:

upload.jsp

<%@ page contentType="text/html; charset=UTF-8" language="java" errorPage="error.jsp" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>通过代码实现文件类型过滤</title>
</head>
<body>
<span style="color:red">${requestScope.typeError}</span>
<form action="upload.action" method="post"
enctype="multipart/form-data">
   选择文件:<input type="file" name="upload" /><br />
<input value="上传" type="submit" />
</form>
</body>
</html>


struts.xml<此处仅列出需要的那部分代码>

<action name="upload" class="org.com.action.UploadAction">
<!-- 设置允许上传的文件类型 -->
<param name="allowTypes">
image/bmp,image/jpg,image/x-png,image/gif,image/jpeg,text/plain,application/msword,application/excel,application/vnd.ms-excel,application/vnd.ms-powerpoint,application/zip,application/x-zip-compressed,application/rar,application/octet-stream,application/xml,text/xml</param>
<!-- 动态设置Action的属性值 -->
<param name="savePath">/upload</param>
<!-- 上传失败后退回input逻辑视图 -->
<result name="input">/news/upload.jsp</result>
<!-- 配置Struts 2默认的视图页面 -->
<result>/news/uploadlist.jsp</result>
</action>

UploadAction.java

package org.com.action;

import com.opensymphony.xwork2.*;
import org.apache.struts2.ServletActionContext;
import java.io.File;
import java.io.*;
import org.springframework.stereotype.Service;

@Service()
public class UploadAction extends ActionSupport {
private File upload;
private String uploadContentType;
private String uploadFileName;
private String savePath;

public void setSavePath(String value) {
this.savePath = value;
}

private String getSavePath() throws Exception {
return ServletActionContext.getRequest().getRealPath(savePath);
}

private String allowTypes;

public String getAllowTypes() {
return allowTypes;
}

public void setAllowTypes(String allowTypes) {
this.allowTypes = allowTypes;
}

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

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

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

public File getUpload() {
return (this.upload);
}

public String getUploadContentType() {
return (this.uploadContentType);
}

public String getUploadFileName() {
return (this.uploadFileName);
}

public String filterType(String[] types) {
String fileType = getUploadContentType();
for (String type : types) {
if (type.equals(fileType)) {
return null;
}
}
return INPUT;
}

@Override
public String execute() throws Exception {
String filterResult = filterType(getAllowTypes().split(","));
if (filterResult != null) {
ActionContext.getContext().put("typeError", "您要上传的文件类型不正确!");
return filterResult;
}
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\"
+ getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
return SUCCESS;
}

uploadlist.jsp


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<html>
<body>
文件已保存到:<%=basePath + "upload/"%><s:property value="uploadFileName" />
<br />
点此下载:
<a href=<%=basePath + "upload/"%> <s:property value="uploadFileName" />><s:property
value="uploadFileName" />
</a>
</body>
</html>

还未解决的问题:虽然我在struts.xml里面配置了能够上传rar和zip文件,但实际还是不能上传。这个问题产生的具体原因还未找到。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值