当前位置: 首页 > news >正文

struts实现的图片的上传和下载

 这是struts实现的图片的上传和下载的关键代码:

Java code
   
public class FileAction extends DispatchAction { private JDBConnection connection = new JDBConnection(); // 以下方法实现文件的上传 public ActionForward upLoadFile(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { Date date = new Date(); FileActionForm fileActionForm = (FileActionForm) form; FormFile file = fileActionForm.getFile(); // 获取当前的文件 String dir = servlet.getServletContext().getRealPath( " /image " ); String path = upload(dir, file); // 实现文件的上传的功能,并且返回上传服务器的路径 path = " image/ " + path; String fileName = Chinese.toChinese(fileActionForm.getFileName()); // 获取文件的名称 String fileSize = String.valueOf(file.getFileSize()); String fileDate = DateFormat.getDateInstance().format(date); String sql = " insert into tb_file values(' " + fileName + " ',' " + fileSize + " ',' " + fileDate + " ',' " + path + " ') " ; connection.executeUpdate(sql); connection.closeConnection(); return mapping.findForward( " upLoadFileResult " ); } public String upload(String dir, FormFile formFile) throws Exception { Date date = new Date(); // 取欲上传的文件的名字和长度 String fname = formFile.getFileName(); // 将上传时间加入文件名 int i = fname.indexOf( " . " ); String name = String.valueOf(date.getTime()); String type = fname.substring(i + 1 ); fname = name + " . " + type; InputStream streamIn = formFile.getInputStream(); // 创建读取用户上传文件的对象 File uploadFile = new File(dir); // 创建把上传数据写到目标文件的对象 if ( ! uploadFile.exists() || uploadFile == null ) { // 判断指定路径是否存在,不存在则创建路径 uploadFile.mkdirs(); } String path = uploadFile.getPath() + " / " + fname; OutputStream streamOut = new FileOutputStream(path); int bytesRead = 0 ; byte [] buffer = new byte [ 8192 ]; while ((bytesRead = streamIn.read(buffer, 0 , 8192 )) != - 1 ) { streamOut.write(buffer, 0 , bytesRead); } streamOut.close(); streamIn.close(); formFile.destroy(); return fname; } // 实现文件的下载 public ActionForward downFile(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String path = request.getParameter( " path " ); BufferedInputStream bis = null ; BufferedOutputStream bos = null ; OutputStream fos = null ; InputStream fis = null ; String filepath = servlet.getServletContext().getRealPath( " / " + path); File uploadFile = new File(filepath); fis = new FileInputStream(uploadFile); bis = new BufferedInputStream(fis); fos = response.getOutputStream(); bos = new BufferedOutputStream(fos); response.setHeader( " Content-disposition " , " attachment;filename= " + URLEncoder.encode(path, " utf-8 " )); int bytesRead = 0 ; byte [] buffer = new byte [ 8192 ]; while ((bytesRead = bis.read(buffer, 0 , 8192 )) != - 1 ) { bos.write(buffer, 0 , bytesRead); } bos.flush(); fis.close(); bis.close(); fos.close(); bos.close(); return null ; } }

相关文章:

  • JavaMail无法连接SMTP25端口问题
  • Project property XFire services.xml path does not refer to a valid file解决办法
  • 控制fckeditor文本长度
  • 有关hibernate配置
  • js代码总结
  • url中的jsessionid
  • 精妙SQL语句
  • Tomcat及MySQL服务配置
  • document.referrer 用法
  • ssh框架有关数据源的配置
  • java解析xml的四种方法
  • double类型的数字变为汉字(金额)
  • 特长的string从一行变多行
  • SESSIONS.ser 的问题
  • 有关urlrewrite
  • “寒冬”下的金三银四跳槽季来了,帮你客观分析一下局面
  • 【挥舞JS】JS实现继承,封装一个extends方法
  • CentOS6 编译安装 redis-3.2.3
  • HTTP那些事
  • Laravel Mix运行时关于es2015报错解决方案
  • magento 货币换算
  • MYSQL 的 IF 函数
  • Next.js之基础概念(二)
  • node入门
  • Vue UI框架库开发介绍
  • WebSocket使用
  • 安卓应用性能调试和优化经验分享
  • 从 Android Sample ApiDemos 中学习 android.animation API 的用法
  • 分布式任务队列Celery
  • 工作踩坑系列——https访问遇到“已阻止载入混合活动内容”
  • 浅析微信支付:申请退款、退款回调接口、查询退款
  • 移动端唤起键盘时取消position:fixed定位
  • ​RecSys 2022 | 面向人岗匹配的双向选择偏好建模
  • ()、[]、{}、(())、[[]]等各种括号的使用
  • (1)(1.9) MSP (version 4.2)
  • (1/2)敏捷实践指南 Agile Practice Guide ([美] Project Management institute 著)
  • (NSDate) 时间 (time )比较
  • (八)五种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (附源码)spring boot北京冬奥会志愿者报名系统 毕业设计 150947
  • (附源码)spring boot基于Java的电影院售票与管理系统毕业设计 011449
  • (转)一些感悟
  • (自适应手机端)响应式新闻博客知识类pbootcms网站模板 自媒体运营博客网站源码下载
  • .Net Core和.Net Standard直观理解
  • .NET 实现 NTFS 文件系统的硬链接 mklink /J(Junction)
  • .net 微服务 服务保护 自动重试 Polly
  • .NET成年了,然后呢?
  • .net反编译的九款神器
  • .NET平台开源项目速览(15)文档数据库RavenDB-介绍与初体验
  • /3GB和/USERVA开关
  • ::before和::after 常见的用法
  • @Autowired标签与 @Resource标签 的区别
  • @DataRedisTest测试redis从未如此丝滑
  • @param注解什么意思_9000字,通俗易懂的讲解下Java注解
  • [ vulhub漏洞复现篇 ] Jetty WEB-INF 文件读取复现CVE-2021-34429
  • [【JSON2WEB】 13 基于REST2SQL 和 Amis 的 SQL 查询分析器