struts实现的图片的上传和下载
来源:互联网 发布:最新免费读书软件 编辑:程序博客网 时间:2024/04/30 15:00
这是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; }}
- struts实现的图片的上传和下载
- struts+jsp在页面图片的上传和下载
- struts:实现图片的上传
- Struts的上传和下载
- untiy实现图片的上传和下载
- jsp实现图片的上传和下载
- Unity实现图片的上传和下载
- 用struts实现文件的上传和下载
- struts的上传、下载
- struts中文件的上传和下载
- struts文件上传和下载的
- Struts的文件上传和下载
- 【Struts】文件的上传和下载
- Android通过WebService实现图片的上传和下载(一)
- MVC图片的上传和下载(简单实现)
- php程序中实现图片的上传和下载
- spring boot实现图片的上传和下载功能
- struts实现文件上传和下载
- Tapestry4常用的注入对象
- Kook ajax v1.0
- 学习SYMBIAN 对话框(三)
- 利用動態 Script 讓 Local 端的 HTML 跨網域存取 Server 上的資料
- D3D9 SDK文档笔记4---一切从头开始(3)
- struts实现的图片的上传和下载
- occi的例子2(代码)
- 在linux用dig检测dns解析
- Webservice中如何实现方法重载(overload)以及如何传送不能序列化的对象作参数
- System.getProperty
- 简化 Ajax 和 Java 开发,第 1 部分: 用 JSP 标记文件动态生成 JavaScript 代码
- socket编程与线程模型一
- 如何用Procexp和Autoruns识别并删除木马
- occi 标准连接数据库代码