Java Spring使用Simple-Ajax-Uploader上传组件上传文件

单于飞鸣
2023-12-01

Service片段


    public FileUploadResult uploader(MultipartFile recommondImage, HttpServletRequest req) {
        FileUploadResult fileResult = new FileUploadResult();
        InputStream inputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            String originalFilename = recommondImage.getOriginalFilename();
            String realPath = req.getServletContext().getRealPath("/static/upload/") + File.separator;
            File uploadDir = new File(realPath);
            if (!uploadDir.exists()) {
                uploadDir.mkdir();
            }
            String fileSuffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            String fileName = UUID.randomUUID().toString() + fileSuffix;
            inputStream = recommondImage.getInputStream();
            fileOutputStream = new FileOutputStream(realPath + "/" + fileName);
            int length = 0;
            byte[] bytes = new byte[3 * 1024];
            int temp;
            while ((length = inputStream.read(bytes)) != (-1)) {
                fileOutputStream.write(bytes, 0, length);
            }
            fileResult.setMessage("图片上传成功!");
            fileResult.setError(0);
        } catch (IOException | NullPointerException e) {
            fileResult.setError(1);
            fileResult.setMessage("图片上传失败!");
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return fileResult;
    }

FileUploadResult实体

    class FileUploadResult {
        private int error;
        private String message;

        public int getError() {
            return error;
        }

        public void setError(int error) {
            this.error = error;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }
}

JSP

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>ajax上传测试</title>
        <script src="<%=request.getContextPath()%>/static/js/SimpleAjaxUploader.js"></script>
    </head>
    <body>
            <form method="post" enctype="multipart/form-data">
                <input type="file" accept="image/*" id="img-input"
                       name="imgFile" onchange="uploadImg(this)">
            </form>
    </body>
    <script>
        function uploadImg(file) {
            //AJAX上传图片
            var $photo = $(file);
            var uploader = new ss.SimpleUpload({
                button: $photo[0],
                url: <%=request.getContextPath()%> +'/FileUploader/upload',
                name: 'photo',
                responseType: 'json',
                allowedExtensions: ['jpg', 'jpeg', 'png', 'JPG', 'JPEG', 'PNG'],
                multipart: true,
                onComplete: function (filename, response) {
                    console.info(response);
                }
            });
        }
    </script>
</html>

Simple-Ajax-Uploader上传组件github地址
用于跨浏览器Ajax文件上传的Javascript插件。支持拖放,CORS和带进度条的多文件上传。适用于IE7-9,移动设备和所有现代浏览器。

 类似资料: