struts2 上传下载

[color=brown]上传.jsp 部分代码[/color]

<td >上传文件路径:</td>
<td >
<s:file name="[color=brown]file[/color]" id="Jiafile"/>
</td>


// 简单判断上传路径是否有效 form的name=“forms”
function sub(){
var filespec = document.forms[0].file.value;
var patrn=/^[C|D|E|F]:\\.+$/;
if (!patrn.exec(filespec)){
alert( "上传路径不正确!");
return ;
} else{
document.forms[0].action="upload.action";
document.forms[0].submit();
}
}



[color=brown]uploadAction.java[/color]

private java.io.File  file;  //file与jsp中file的name相同
private String fileFileName; // file的属性 上传文件的名字
private String path="/upload"; // WebRoot下文件夹upload

public String upload() {

//上传到文件夹中的名字 防止文件名重复 覆盖
String xname=UUID.randomUUID().toString()+fileFileName.substring( fileFileName.lastIndexOf("."));

//数据存库
.....

//上传到相应的文件夹
this.send(file, xname );

return SUCCESS;
}


/**
* name 上传到文件夹中的 附件的名称
*/
public void send( File file , String name) throws Exception {

//以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(
ServletActionContext.getServletContext().getRealPath(path) + "\\" + name);
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0)
{
fos.write(buffer , 0 , len);
}
fos.close();
fis.close();

}


[color=red]下载[/color]
 private String inputPath ;
private String filename;
private String id ;


public InputStream getInputStream() throws Exception {
return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
}

public String downloadFile() throws IOException {
FileManager service = new FileManager(this.getLoginId());
cn.com.harbor.oa.file.entity.File oaFile = service.getFileById(id);
oaFile.setNumOfDownload(oaFile.getNumOfDownload() + 1);
service.update(oaFile);

inputPath="/user/file/upload/"+oaFile.getXname();//要下载的文件名称
this.filename= new String( oaFile.getName().getBytes("GBK"),"ISO8859-1"); //保存文件时的名称

HttpServletResponse response = ServletActionContext.getResponse();
return null;
}


<action name="downloadFile" class="cn.com.harbor.oa.file.FileUploadAction" method="downloadFile">
<result name="success" type="stream">
<!-- param name="contentType">${contentType}</param-->
<param name="inputName">inputStream</param>
<param name="bufferSize">4096</param>
<param name="contentDisposition">attachment;filename="${filename}"</param>
<param name="">/user/file/fileUpload.jsp</param>
</result>

</action>


//删除
/**
* 删除文件夹内容
*/
public boolean delPic( String xname )
{
File file = new File(ServletActionContext.getServletContext().getRealPath(path)+"/"+xname);
if(!file.exists()) {
return false;
} else {
if(file.exists() && file.isFile()) return file.delete();
else return false;
}
}



//下载取消报错
<constant name="struts.multipart.maxSize" value="9000000"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值