struts2文档上传与下载

页面(freemark页面):
<input type="file" id="filePath" name="filePath" value="${filePath?if_exists}" class="textbox_css" style="width:500px" />
   <a href="${base}/system/batchDomnManager.action?act=download"><font color="blue">excle模板下载</font></a>

<input type="button" class="btn" name="btnUpload" value="附件上传" οnclick="addOperatorUpload();" />

//文件上传操作
function addOperatorUpload(){
var filepath=document.getElementById("filePath").value;
if(''==filepath){
alert("请选择要上传的excle文件");
return;
}
document.location.href="${base}/system/operatorBatchManager.action?act=doOther&filePath="+filepath;
}

**********************************************************************
action:
/**
* 文件的路径,页面中只要写name="filePath",这样后台就可以拿到值啦
*/
private String filePath;
/**
* 文件
*/
private File myFile;

// myFileContentType属性用来封装上传文件的类型
private String myFileContentType;

// myFileFileName属性用来封装上传文件的文件名
private String myFileFileName;

private String fileName;// 初始的通过param指定的文件名属性

private String inputPath;// 指定要被下载的文件路径
//以上get,set略


//以下是文件下载方法
public String execute() throws Exception {
if("download".equals(this.act)){
return SUCCESS;
}else{
return super.execute();
}
}

public InputStream getInputStream() throws Exception {

// 文件下载目录路径
String downloadDir = ServletActionContext.getServletContext()
.getRealPath("/upload");
// 文件下载路径
String downloadFile = ServletActionContext.getServletContext()
.getRealPath(inputPath);
File file = new File(downloadDir);
// 真实文件路径,去掉里面的..等信息
downloadFile = file.getCanonicalPath();
// 发现企图下载不在/upload下的文件,就显增空内容
if (!downloadFile.startsWith(downloadDir)) {
return null;
}
// 通过 ServletContext,也就是application 来读取数据
return ServletActionContext.getServletContext().getResourceAsStream(
inputPath);
}

/**
* 提供转换编码后的供下载用的文件名
*/
public String getDownloadFileName() {
String downFileName = fileName;
try {
downFileName = new String(downFileName.getBytes(), "GBK");
} catch (Exception e) {
e.printStackTrace();
}
return downFileName;
}

//以下是文件上传方法
public String doOther() throws Exception {

myFile = new File(filePath);
// 获得文件名
String type = myFile.getName();// this.getMyFileFileName();
int i = 0;
while (i != -1) {
// 找到上传文件的类型的位置,这个地方的是'.'
i = type.indexOf(".");
// 截取上传文件的后缀名,此时得到了文件的类型
type = type.substring(i + 1);
}
// 如果是excle文件就开始处理
if ("xls".equals(type) || "xlsx".equals(type)) {
// 获得文件路径
String str_path = myFile.toString();
this.excelDataImportOracle(str_path);
} else {
// 文件类型不对
this.setErrorInfo(getText("info.fileErrorType"));
}
return "addInput";
}

public void excelDataImportOracle(String pathname) throws Exception {
try {

/** 上传文件到服务器 */
// 得到文件存放的目录路径
String dir = ServletActionContext.getServletContext().getRealPath(
"/upload");
String fileName = myFile.getName();
String targetFileName = generateFileName(fileName);

InputStream is = new FileInputStream(myFile);
// 设置目标文件
File toFile = new File(dir, targetFileName);
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(toFile));

byte[] b = new byte[20480];
int length = 0;
while ((length = is.read(b)) != -1) {
bos.write(b, 0, length);
bos.flush();
}
bos.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}

*********************************************************************
struts.xml配置文件:
<!-- 批量用户管理-->
<action name="operatorBatchManager" class="com.tempus.fw.operatorBatchAction">
<result name="success" type="redirect">
operatorSearch.action?act=doquery&busiId=searchOperator
</result>
<result name="addInput" type="freemarker">
/WEB-INF/core/pages/system/operatorBatchOpt.ftl
</result>
</action>

<!-- 批量用户模板下载管理-->
<action name="batchDomnManager" class="com.tempus.fw.operatorBatchAction">
<param name="inputPath">/upload/mould.xls</param>
<!-- 初始文件名 -->
<param name="fileName">mould.xls</param>
<!-- 下載配置 -->
<result name="success" type="stream">
<!-- 下載類型和編碼配置 -->
<param name="contentType">application/octet-stream;charset=GBK</param>
<!-- action中提供输入流的属性名称,缺省为inputStream -->
<param name="inputName">inputStream</param>
<!-- 使用经过转码的文件名作为下载文件名,downloadFileName属性 对应action类中的方法 getDownloadFileName() -->
<param name="contentDisposition">attachment;filename="${downloadFileName}"</param>
<!-- 緩存 -->
<param name="bufferSize">4096</param>
</result>
</action>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值