当前位置: 首页 > news >正文

AJAX 文件上传进度条 JAVA

=======JSP文件 

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

<head>
    <meta charset="UTF-8">
    <title>AJAX 文件上传进度条</title>
    <style>
        #smallrainContainer {
            width: 300px;
            border: 1px solid red ;
            margin-top: 10px;
        }
        #smallrainBar {
            height: 20px;
            background-color: blue;
            width: 0%;
        }
    </style>
</head>
<body>
<h3>AJAX 文件上传进度条</h3>


<h3>请选择较大的文件进度条才看的清楚</h3>
    <input type="file" id="smallrainInput" name="smallrainInput">
   
    <div id="smallrainContainer">
        <div id="smallrainBar"></div>
    </div>
 <button onclick="uploadFile()">上传文件</button>

    <script>
        function uploadFile() {
            const fileInput = document.getElementById('smallrainInput');
            const file = fileInput.files[0];
            if (!file) {
                alert('请选择要上传的文件');
                return;
            }

            const formData = new FormData();
            formData.append('smallrainfileInput', file);

            const xhr = new XMLHttpRequest();
            xhr.open('POST', 'smallRainAJAXUpload', true);

            xhr.upload.addEventListener('progress', function (event) {
                if (event.lengthComputable) {
                    const percentComplete = (event.loaded / event.total) * 100;
                    const progressBar = document.getElementById('smallrainBar');
                    progressBar.style.width = percentComplete + '%';
                }
            });
            xhr.onload = function () {
                if (xhr.status === 200) {
                    alert('文件上传成功');
                } else {
                    alert('文件上传失败');
                }
            };

            xhr.send(formData);
        }
    </script>
</body>

</html>

======JAVA文件

 

package org.rain.ajax;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

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("/smallRainAJAXUpload")
@MultipartConfig()
public class MulAJAXUpload extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String shead = new String(
				"<head> <meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\"> </head>");
		out.println("<html>");
		out.print(shead);
		out.println("您访问的路径不存在,非法访问");
		out.println("</body>");
		out.println("</html>");
	}

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

		String savePath = "C:/SmallRian";
		boolean hasFile = false;
		try {
			File fileSaveDir = new File(savePath);
			if (!fileSaveDir.exists()) {
				fileSaveDir.mkdir();
			}

			Part filePart = request.getPart("smallrainfileInput");
			String fileName = filePart.getSubmittedFileName();

			String localFile = savePath + "/" + fileName;
			if (null != fileName && fileName.length() > 0 && filePart.getSize() > 0) {
				filePart.write(localFile);
				hasFile = true;
			} else {
				hasFile = false;
			}

		} catch (IOException e) {
			response.sendError(500);
			throw new ServletException("没有上传成功", e);
		}
	}

}

相关文章:

  • 基于STM32的智能家居蓝牙系统(论文+源码)
  • uni-app 全局请求封装:支持 Promise,自动刷新 Token,解决 401 过期问题
  • 【框架】Spring、SpringBoot和SpringCloud区别
  • c++常用算法
  • 【Linux】消息队列和信号量
  • 深入理解Reactor Flux的生成方法
  • idea显示.java文件不能运行解决方式
  • Java 容器之 List
  • jenkens使用笔记
  • 探索Elasticsearch:认识与安装
  • 不谓侠--记录
  • Hive-08之数据仓库之建模、分析
  • 0111 AI淘金新时代:DeepSeek+工具矩阵的7大变现路径
  • 滞后补偿和超前补偿
  • ctfshow刷题笔记—栈溢出—pwn61~pwn64
  • 物联网小范围高精度GPS使用
  • 华为 Open Gauss 数据库在 Spring Boot 中使用 Flyway
  • 利用@WebMvcTest测试Spring MVC应用
  • 线程 -- 线程池
  • 开学季大学生如何备考微软MOS认证?
  • 网站建设的市场调研分析/全媒体运营师
  • wordpress站标签也打不开/参考消息今天新闻
  • 哪些网站做品牌特卖/东营网站推广公司
  • 南安淘宝网站建设/关键词免费网站
  • 网站推广策划报告航空航天/国外常用的seo站长工具
  • wordpress的论坛主题/奶糖 seo 博客