ajax实现文件上传和表单提交

1. ajax提交表单




    login test
    
    
    
    
    
    
    
    


用户名:

密 码:

 

2. ajax文件上传

前台:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title
    
    


    


后台:

package com.offcn.servlet;

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;
import java.io.IOException;
@MultipartConfig
@WebServlet(name = "TestServlet", urlPatterns = "/TestServlet")
public class TestServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        Part photo = request.getPart("photo");
        String uname = request.getParameter("uname");
        String fileName = photo.getSubmittedFileName();
        photo.write("d://"+fileName);
        System.out.println(uname+"---"+fileName);
    }

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

你可能感兴趣的:(java,ajax,javascript,前端)