zoukankan      html  css  js  c++  java
  • Servlet3.1上传图片示例

    一、前端JSP页面

    <%@page pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Servlet3.1实现文件上传</title>
    </head>
    <body>
    <h1>Servlet3.1实现文件上传</h1>
    <b id="msg" style="color: red;"></b>

    <!-- 表单提交多媒体(此处指图片)必须要加enctype="multipart/form-data"属性和值 -->
    <form name="myform" action="fileUpload1" method="post" enctype="multipart/form-data" onSubmit="return doCheck(this);">
    <table>
    <tr>
    <td>用户名:</td>
    <td><input type="text" name="username" /></td>
    </tr>
    <tr>
    <td>密码:</td>
    <td><input type="password" name="password" /></td>
    </tr>
    <tr>
    <td>头像:</td>
    <td><input type="file" name="photo" /></td>
    </tr>
    <tr>
    <td colspan="2"><input type="submit" value="提交" /></td>
    </tr>
    </table>
    </form>
    <script type="text/javascript">
    var fm = document.forms[0];
    //var form = document.forms['myform'];
    //var form = document.myform;//不能有重名的,否则不能获取到
    var msg = document.getElementById("msg");
    function doCheck(fm) {
    if (null == fm.username.value || "" == fm.username.value) {
    msg.innerHTML = "* 用户名不能为空!";
    fm.username.focus();
    return false;
    }
    if (null == fm.password.value || "" == fm.password.value) {
    msg.innerHTML = "* 密码不能为空!";
    fm.password.focus();
    return false;
    }
    if (null == fm.photo.value || "" == fm.photo.value) {
    msg.innerHTML = "* 上传的文件不能为空!";
    return false;
    }
    }
    </script>
    </body>
    </html>

    二、后台逻辑处理的Servlet

    package com.qubo.servlet3x;

    import java.io.IOException;
    import java.util.Date;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    import javax.servlet.ServletException;
    import javax.servlet.annotation.MultipartConfig;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.Part;

    @WebServlet("/fileUpload1")
    @MultipartConfig
    public class FileUploadServlet1 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    request.setCharacterEncoding("UTF-8");
    response.setContentType("text/html;charset=utf-8");
    String name = request.getParameter("username");
    String password = request.getParameter("password");
    Part part = request.getPart("photo");
    String fileName = part.getSubmittedFileName();//调用获取文件名的方法
    String str = part.getName(); //前端jsp页面中指定的name属性的值,即<input type="file" name="photo" />中的photo
    System.out.println("用户名:" + name + ",密码:" + password + ",文件名:" + fileName + ",图像输入框名:" + str);
    String hzm = fileName.substring(fileName.indexOf("."));//获取文件的后缀名
    part.write("E:/ZhuoXun JavaWeb/Servlet3.x/WebContent/img/" + new Date().getTime() + hzm);//文件的保存位置
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    doGet(request, response);
    }
    }

  • 相关阅读:
    双击bat用CMD窗口打开jar (专为没有界面的java程序设计的) 拂晓风起
    CString 在_UNICODE宏定义下和普通ASCII编码下的不同 拂晓风起
    java web部署 启动线程 (监听socket等) 拂晓风起
    火狐下不错的插件
    JQuery插件,weebox,可移动层,替换模态框
    一些功能不错的开源工具(DNSPod、MailServer、Bug跟踪系统、VS代码检查、VS反翻译)
    用cwRsync同步文件
    DreamWeaver、EditPlus查找两个字符之间字符串的正则表达式
    oarcle hql语句中的 (xx=:名称)hql语句中传值
    this man
  • 原文地址:https://www.cnblogs.com/qubo520/p/7440267.html
Copyright © 2011-2022 走看看