文件上传

简介: 文件上传

编写上传页面


index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <meta charset="UTF-8">
  <title>Insert title here</title>
</head>
<body>
<form action="upload.do" method="post" enctype="multipart/form-data">
  name:<input type="text" name="user">
  选择文件:<input type="file" name="pic">
  <input type="submit" value="提交">
</form>
</body>
</html>


开发处理文件上传的Servlet


1、使用注解@MultipartConfig将一个Servlet标识为支持文件上传。

2、Servlet3.0将multipart/form-data的POST请求封装成Part,通过Part对上传的文件进行操作。

3.使用getSubmittedFileName方法需要tomcat8支持。

  UploadServlet 代码如下:

@WebServlet(name = "UploadServlet",urlPatterns = "/upload.do")
@MultipartConfig
public class UploadServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("utf-8");
        String strUser = request.getParameter("user");
        System.out.println(strUser);
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("UTF-8");
        Part part = request.getPart("pic");
        out.print("文件类型为:" + part.getContentType() + "<br/>");
        out.print("文件的大小为:" + part.getSize() + "<br/>");
        out.print("文件信息为:" + part.getHeader("content-disposition") + "<br/>");
        out.print("文件的名字为:" + part.getSubmittedFileName());
        //得到服务器项目发布运行所在地址
        String strFolder = request.getServletContext().getRealPath("/image")+ File.separator;
        File folder = new File(strFolder);
        if(!folder.exists())
        {
            folder.mkdir();
        }
        //  此处未使用UUID来生成唯一标识,用日期做为标识
        String strNewFilePath = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+ part.getSubmittedFileName();
        String strFinalPath = strFolder + strNewFilePath;
        //查看文件上传路径,方便查找
        System.out.println(strFinalPath);
        part.write(strFinalPath);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
}

ajax上传

<%--
  Created by IntelliJ IDEA.
  User: ttc
  Date: 2018/7/13
  Time: 8:47
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
    <script>
       function upload() {
          var form = new FormData(document.getElementById("upload"));
          var req = new XMLHttpRequest();
          req.open("post", "${pageContext.request.contextPath}/upload.do", true);
          req.send(form);
          req.onload = function () {
//              alert(req.responseText);
              document.getElementById('head').src = '${pageContext.request.contextPath}/upload/'+ req.responseText;
          }
      }
    </script>
  </head>
  <body>
<form id="upload" action="${pageContext.request.contextPath}/upload.do" method="post" enctype="multipart/form-data">
  <img src="${pageContext.request.contextPath}/upload/${picpath}" id="head">
  <input type="text" name="username" value="zhangsan">
  <input type="file" name="file1" onchange="upload();">
  <input type="submit">
</form>
  </body>
</html>



目录
相关文章
|
9月前
|
C#
C#文件上传
C#文件上传
37 0
|
2月前
|
JSON 数据格式
文件上传~~
文件上传~~
30 0
|
10月前
|
JavaScript 前端开发 移动开发
浅谈文件上传
浅谈文件上传
浅谈文件上传
|
9月前
|
存储 移动开发 JavaScript
|
11月前
|
安全 应用服务中间件 PHP
[SUCTF 2019]CheckIn(文件上传)
[SUCTF 2019]CheckIn(文件上传)
112 0
|
前端开发 Java Apache
文件上传与下载
文件上传与下载 文件上传也称为upload,是指将本地图片、视频、音频等文件上传到服务器上,可以供其他用户浏览或下载的过程。文件上传在项目中应用非常广泛,我们经常发微博、发微信朋友圈都用到了文件上传功能。 文件上传时,对页面的form表单有如下要求: method=“post” 采用post方式提交数据 enctype=“multipart/form-data” 采用multipart格式上传文件 type=“file” 使用input的file控件上传
|
开发框架 安全 JavaScript
文件上传利用总结
文件上传利用总结
294 0
|
JavaScript
你真的了解文件上传吗?
你真的了解文件上传吗?
|
数据安全/隐私保护 Windows