分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

struts实现文件上传和下载。

发布时间:2023-09-06 01:36责任编辑:沈小雨关键词:文件上传

先来实现上传。

写上传不管语言,都要先注意前端的form那儿有个细节。

<form name="form1" method="POST" enctype="multipart/form-data">

即这个enctype,编码方式要加上multipart/form-data,这说明了以二进制形式传输数据(即不经过编码处理)。

要是考虑安全性,还要对MIME(Multipurpose Internet Mail Extensions,用了描述因特网内容类型的)作限制,比方说后台只接受MIME类型为图片的(image/jpeg )。

当然这样也还不是很安全,我现在认为比较安全的是,后台接收文件后,截断后缀名,并指定一个后缀名再放到指定文件夹,这样就算有人上传上来了可执行文件,后缀改了 ,那个文件夹不给执行权限,就比较安全了。

struts.xml配置

<action name="student_uploadClasSchedule" class="com.studentweb.action.StudentAction" method="uploadClasSchedule"> ???<!-- 下面这句即可以做到MIME类型判断 --> ???<param name="fileUpload.allowedTypes">text/plain</param> ???<!-- 指定文件保存路径 --> ???<param name="savePath">classchedule</param> ???<result name="SUCCESS" type="redirect">student_showClasSchedule.do</result></action>

action处理

 ???public String uploadClasSchedule() { ???????uploadfile(file); ???????// 下面几句只是数据库处理,不用在意,文件名等等肯定得存数据库里的。 ???????ClasSchedule cs = new ClasSchedule(); ???????cs.setFilename(fileFileName); ???????std.addClasSchedule(cs); ???????return SUCCESS; ???} ???public void uploadfile(File file) { ???????/**
???????* getRealPath方法就是拿到存储位置的绝对路径,savaPath是在struts.xml里配置好的。
     * File.separator是操作系统的分隔符,linux/unix --- "/" ,windows --- "\\"
???????*/ ???????final String dstPath = ServletActionContext.getServletContext().getRealPath(this.getSavePath()) + File.separator ???????????????+ fileFileName; ???????try {
???????????//真正把文件保存到本地是这句。 ???????????FileUtils.copyFile(file, new File(dstPath)); ???????} catch (IOException e) { ???????????// TODO Auto-generated catch block ???????????e.printStackTrace(); ???????} ???} ???public String getSavePath() { ???????return savePath; ???}

下面是下载

struts.xml配置

<action name="student_downloadcs" class="com.studentweb.action.StudentAction" method="downloadcs"> ????<result name="success" type="stream">
????????<!-- 这儿设置下载文件的MIME类型,此处为docx类型,需要自行搜索配置。也可设置编码 --> ????????<param name="contentType">application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=ISO-8859-1</param>
????????<!--下载文件的来源流,对应着action类中某个类型为Inputstream的属性名,例如取值为targetFile的属性需要编写getTargetFile()方法  --> ????????<param name="inputName">targetFile</param>
      <!-- attachment这个设置即提醒浏览器弹出下载或直接打开的框,fileName即返回处理过的文件名(解决中文乱码) --> ????????<param name="contentDisposition">attachment;filename="${fileName}"</param> ????????<param name="buffersize">4096</param> ???</result></action>

后台处理

 ???public String downloadcs() { ???????return SUCCESS; ???} ???????public InputStream getTargetFile() { ???????String savePath = "classchedule"; ???????String temp = request.getParameter("filename"); ???????savePath += File.separator + temp;
???????// 即以流的形式返回 ???????return ServletActionContext.getServletContext().getResourceAsStream(savePath); ???} ???????????public String getFileName() {
???????//接受要下载的文件名 ???????String temp = request.getParameter("filename"); ???????try { ?????????temp = ?new String(temp.getBytes(), "ISO-8859-1"); ??//解决下载时中文乱码 ???????} catch (UnsupportedEncodingException e) { ???????????// TODO Auto-generated catch block ???????????e.printStackTrace(); ???????} ?????????return temp; ???}

顺便说一下删除吧,主要用的是java.io.File.File里面的方法。

后台处理,接受文件id删除的方式。

 ???public String deleteCS() { ???????String temp = request.getParameter("id"); ???????int id = Integer.valueOf(temp);
???????//这儿是删除数据库里的信息 ???????String filename = std.getFile(id); ???????std.deleteFile(id);
     //这儿是删除实际存储的文件。 ???????String dstPath = ServletActionContext.getServletContext().getRealPath("/classchedule") + File.separator ???????????????+ filename; ???????File file = new File(dstPath); ???????System.out.println(dstPath+"////"+file.exists()); ???????if(file.exists()) ???????????file.delete(); ???????return "DELETEFILE"; ???}
/** file常用方法
* ??file.exists() 判断文件是否存在
* ??file.delete() 删除文件
* ??file.getParentFile().mkdirs() 在上层文件创建目录
* ??file.createNewFile() ?创建当前文件(在存储中)
*/
?
 

struts实现文件上传和下载。

原文地址:https://www.cnblogs.com/NoYone/p/8278916.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved