表单文件上传与文件下载

一、简介

  使用form表单进行需要为form添加enctype="multipart/form-data" 属性,除此之外还需要将表单的提交方法改成post,如下 method="post"。

二、示例

  1、表单文件上传

  网页代码如下:

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 </head>
 5 <body>
 6     <form action="${pageContext.request.contextPath}/file/upload.action" method="post" enctype="multipart/form-data">
 7         <div id="contentTable" style="border: 0px;">
 8             <h1 class="title" style="font-size: 15px; border-bottom: 1px solid #DFE3E6;">导入数据</h1>
 9             <table width="80%">
10             <tr>
11                  <td width="20%"align="right">
12                 选择要上传的文件
13                   </td>
14                 <td width="70%" id="name_h" title="" style="text-align: center;">
15                 <input type="file"  name="xlsfile" id="xlsfile" />
16                 </td>
17                 </tr>
18             </table>
19             <div id="activityTable">
20                  <input id="btnSave"  type="submit" value="导&nbsp;入" />
21             </div>
22         </div>
23     </form>
24 </body>
25 </html>

  后端上传处理代码:

 1     /**
 2     *使用springmvc处理文件上传
 3     */
 4     @RequestMapping("upload")
 5     @ResponseBody
 6     public boolean upload(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws UnsupportedEncodingException {
 7         String path = request.getSession().getServletContext().getRealPath("");
 8         Calendar calendar = Calendar.getInstance();
 9         calendar.setTime(new Date());
10         request.setCharacterEncoding("UTF-8");
11         path = String.format("%s%s%s\\%s\\%s\\%s", path, "upload", "file", calendar.get(calendar.YEAR),
12                 calendar.get(calendar.MONTH), calendar.get(calendar.DAY_OF_MONTH));
13         File filepath = new File(path);
14         if (!filepath.exists()) {
15             filepath.mkdirs();
16         }
17          MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
18         获得文件
19         MultipartFile multipartFile = multipartRequest.getFile("xlsfile");
20         OutputStream os = null;
21         InputStream is = null;
22         File uploadFile = null;
23         try {
24             is = multipartFile.getInputStream();
25             uploadFile = new File(filepath, System.currentTimeMillis() + ".xls");
26             os = new FileOutputStream(uploadFile);
27             IOUtils.copy(is, os);//使用commons-io组件进行文件流的处理
28             os.flush();
29         } catch (IOException e) {
30             e.printStackTrace();
31             return false;
32         }finally{
33             IOUtils.closeQuietly(os);
34             IOUtils.closeQuietly(is);
35         }
36     return true;
37 }

  2、文件下载

 1     /**
 2     *使用springmvc进行文件下载处理
 3     */
 4     @RequestMapping({ "/template" })
 5     public void downloadTemplate(HttpServletRequest request, HttpServletResponse response)
 6             throws UnsupportedEncodingException {
 7         String path = request.getSession().getServletContext().getRealPath("");
 8         String filename = "模板文件.xls";
 9         File file = new File(path +  "\\file\\templagte\\" + filename);
10         String userAgent = request.getHeader("User-Agent");
11         byte[] bytes = userAgent.contains("MSIE") ? filename.getBytes() : filename.getBytes("UTF-8"); // fileName.getBytes("UTF-8")处理safari的乱码问题
12         String fileName = new String(bytes, "ISO-8859-1");
13         // 设置输出的格式
14         response.setContentType("multipart/form-data");
15         response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
16
17         InputStream inStream = null;
18         try {
19             inStream = new FileInputStream(file);
20             IOUtils.copy(inStream, response.getOutputStream());//使用commons-io组件进行文件流的处理
21         } catch (IOException e) {
22             e.printStackTrace();
23         }finally{
24             IOUtils.closeQuietly(inStream);
25         }
时间: 2024-08-06 23:47:11

表单文件上传与文件下载的相关文章

Ajax(form表单文件上传、请求头之contentType、Ajax传递json数据)

form表单文件上传 上菜 file_put.html <form action="" method="post" enctype="multipart/form-data"> {# 这里必须要请求头格式才能把上传文件的对象传过去 enctype="multipart/form-data"#} 姓名 <input type="text" name="user">

混合表单文件上传到数据库(基于TOMCAT)

在实际的开发中在实现文件上传的同时肯定还有其他信息需要保存到数据库,就像混合表单在上传完毕之后需要将提交的基本信息插入数据库. 在这个demo中需要用到这个架包来帮助实现 1.定义一个公共类实现文件上传(BaseServlet) 上传是一个公共的操作,可能在很多个servlet中都要涉及到上传,比如在Empservlet中要上传雇员的照片,在ManagerServlet中要上传管理员的照片,此时就需要将上传的操作定义到一个公共父类. package com.sxt.mvcpro.servlet;

如何使用PHP上传文件,上传图片,php上传教程,php表单文件上传教程

使用PHP进行文件上传,主要使用到表单功能和PHP内置的$_FILES函数功能.接下来我们看如何实现PHP上传功能.例子效果图,此例子是在Mac下进行调试成功的. PHP上传图片文件的功能代码如下: <html> <head> <meta charset="utf-8"> <title>Upload File Example</title> <style> body{ width:500px; margin:20p

表单+文件上传+音频+iframe

一.表单及其标签 首先书写结构 <form action="表单提交地址" menthod=“post get提交方式”>  form只是外边框 <inpu type=“表单元素的类型” name=“键” value=“值”>                           表单元素: 文本类型: 文本框:text   可不规定value值 规定即锁定数值 密码框:password   可不规定value值 隐藏域:hidden 多行文本:textarea

文件上传(表单文件上传)

文件上传是开发一个网站最基本的一个需求功能 前台页面的设置: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerNa

from表单文件上传后页面跳转解决办法

from表单上传文件,路径跳转后,又不能转发回来. 本人的一个解决办法是.返回一段html代码,浏览器解析后后退一步,回到原来的页面并刷新. return "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/></head><body onload='JavaScript:history.

浅析http请求头常见的表单文件上传

首先先了解下application/x-www-form-urlencoded和multipart/form-data的区别 application/x-www-form-urlencoded: 是常用的表单发包方式,普通的表单提交,或者js发包,默认都是通过这种方式, <form enctype="application/x-www-form-urlencoded" action="http://" method="POST"> &

form表单文件上传

创建一个form表单 #上传文件的时候,enctype属性改成multipart/form-data <from action='' method='post' enctype='multipart/form-data'> <input type='file' name='avatar'> <input type='submit'> </from> 服务器接收 #服务器接收的时候,文件被放在request.FILES中 def file_put(reques

CURL模拟表单post提交及相关常用参数的使用(包括提交表单同时上传文件)

转载自:https://blog.csdn.net/freedomwjx/article/details/43278157 一. 首先,最简单的情况是我们只需要提交一个不带文件上传的表单,这种情况下,只需要在curl中使用--data(注意是--不是-)或者它的缩写-d即可. [plain] view plain copy curl -d "key=value&key=value" "url" 或者 [plain] view plain copy curl