分享web开发知识

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

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

struts2实现文件上传下载

发布时间:2023-09-06 02:12责任编辑:傅花花关键词:文件上传

代码比较粗糙,这里只做个记录,方便日后查询

/** * 上传文件代码 */public class UploadAndDownloadAction extends ActionSupport{ ????// 获取文件 ???private File myFile; ???// 获取文件的名称 ???private String myFileFileName; ???// 获取文件的类型 ???private String myFileContentType; ???public File getMyFile() { ???????return myFile; ???} ???public void setMyFile(File myFile) { ???????this.myFile = myFile; ???} ???public String getMyFileFileName() { ???????return myFileFileName; ???} ???public void setMyFileFileName(String myFileFileName) { ???????this.myFileFileName = myFileFileName; ???} ???public String getMyFileContentType() { ???????return myFileContentType; ???} ???public void setMyFileContentType(String myFileContentType) { ???????this.myFileContentType = myFileContentType; ???} ???????????public boolean upload() { ???????????????????????// 1.确定上传的文件保存的位置 ????????????String uploadRootPath = "/common/download/template"; ????????????String dir = Struts2Util.getRequest().getRealPath(uploadRootPath); ???????????// 2.判断这个文件夹是否存在,不存在创建 ???????????File file = new File(dir); ???????????if (!file.exists()) { ???????????????file.mkdir(); ???????????} ???????????// 3.将文件放到InputStream里面,然后outputstream写入文件 ???????????InputStream is = null; ???????????OutputStream os = null; ???????????try { ???????????????is = new BufferedInputStream(new FileInputStream(myFile)); ???????????????os = new BufferedOutputStream(new FileOutputStream(dir + "\\" + myFileFileName)); ???????????????byte[] buffer = new byte[1024]; ???????????????int byteLen = 0; ???????????????while ((byteLen = is.read(buffer)) > 0) { ???????????????????os.write(buffer, 0, byteLen); ???????????????} ???????????????domain.setTemplateName(myFileFileName); ???????????????domain.setTemplateUrl(uploadRootPath+ "\\" + myFileFileName); ???????????????domain.setTemplateFullPath(dir+ "\\" + myFileFileName); ???????????????????????????} catch (Exception e) { ???????????????????????????} finally { ???????????????if (is != null) { ???????????????????try { ???????????????????????is.close(); ???????????????????} catch (IOException e) { ???????????????????} ???????????????} ???????????????if (os != null) { ???????????????????try { ???????????????????????os.close(); ???????????????????} catch (IOException e) { ???????????????????} ???????????????} ???????????} ???????????return true; ???????}}
/** * ?文件下载 */public class UploadAndDownloadAction extends ActionSupport ?{ ????????????private String fileName; ???????public String getFileName() { ????????try{ ????????????fileName = URLEncoder.encode(fileName,"UTF-8"); ????????}catch(Exception e){ ????????????throw new RuntimeException(); ????????} ?????????????????return fileName; ???} ???public void setFileName(String fileName) { ???????this.fileName = fileName; ???} ????????//2.3:返回流的方法 下载 ????public InputStream getAttrInputStream() throws ItsException, UnsupportedEncodingException{ ????????String fileName=Struts2Util.getRequest().getParameter("fileName"); ????????????this.setFileName(fileName); ????????????InputStream is=ServletActionContext.getServletContext().getResourceAsStream("路径"); ????????????return is; ????????}else{ ????????????return null; ????????} ????} ??????????????public String getDownloadFile() throws ItsException { ????????return "success"; ????} ?????????/** ?????* ?struts2 配置 ?????*/ ????<action name="uploadAndDownload*" class="uploadAndDownloadAction" method="{1}"> ???????????<result name="uploadTemplate">/common/download/jsp/uploadTemplate.jsp</result> ???????????<!-- 下载操作 --> ?????????????<result name="success" type="stream"> ????????????????<param name="contentType">application/octet-stream</param> ????????????????<param name="inputName">attrInputStream</param> ????????????????<param name="contentDisposition">attachment;filename="${fileName}"</param> ????????????????<param name="bufferSize">1024</param> ???????????????????????</result> ???</action>}
/** * 删除文件 */public static boolean delete(String path) { ???File file = new File(path); ???if (!file.exists()) { ???????return false; ???}else{ ???????return deleteFile(path); ???}}/** * 删除文件 */public static boolean deleteFile(String path) { ???File file = new File(path); ???// 如果文件路径所对应的文件存在,并且是一个文件,则直接删除 ???if (file.delete()) { ???????return true; ???}else { ???????return false; ???}}

struts2实现文件上传下载

原文地址:https://www.cnblogs.com/jiaobaobao/p/9565195.html

知识推荐

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