有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java上传的图像没有存储在spring的目录中

我正试图上传一张图片并将其保存到一个特定的文件夹中。这是我的控制器:

 @RequestMapping(value = "/echofile",method = RequestMethod.POST)
        public @ResponseBody HashMap<String, Object> echoFile(HttpServletRequest  request, HttpSession session,
                HttpServletResponse response ,  @ModelAttribute("uploadedFile") UploadedFile upldfile) throws Exception {
        HashMap<String, Object> map = new HashMap<String, Object>();

         if(request instanceof MultipartHttpServletRequest){

               InputStream inputStream = null;
                  OutputStream outputStream = null;
                MultipartFile multipartFile = ((MultipartRequest) request).getFile("file");

                MultipartFile file = upldfile.getFile();
                String fileName = file.getOriginalFilename();

                System.out.println("filename:"+fileName);
                upldfile.setFile(file);
                String fileName2 = multipartFile.getOriginalFilename();
                System.out.println("filename2:"+fileName2);

                Long size = multipartFile.getSize();
                String contentType = multipartFile.getContentType();
                InputStream stream = multipartFile.getInputStream();
                byte[] bytes = IOUtils.toByteArray(stream);


                map.put("fileoriginalsize", size);
                map.put("contenttype", contentType);
                map.put("base64", new String(Base64Utils.encode(bytes)));

                try {
                      // inputStream = ((MultipartFile) upldfile).getInputStream();

                    inputStream = file.getInputStream();
                        String webRootDir = session.getServletContext().getRealPath("/");
                        String url = "E:/Java_Project/EmployeeOnlineRegistrationForm/src/main/webapp/resources/image";

                        String pathFl = url + File.separator ;
                       File newFile = new File(webRootDir+pathFl+fileName);
                       if (!newFile.exists()) {
                        newFile.createNewFile();
                       }
                       outputStream = new FileOutputStream(newFile);
                       int read = 0;
                      // byte[] bytes = new byte[1024];

                       while ((read = inputStream.read(bytes)) != -1) {
                        outputStream.write(bytes, 0, read);
                       }
                      } catch (IOException e) {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
                      }


            }

         return map;
     }

This is my JS ajax part:

var isJpg = function(name) {
    return name.match(/jpg$/i)
};

    var isPng = function(name) {
        return name.match(/png$/i)
    };

    $(document).ready(function() {
        var file = $('[name="file"]');
        var imgContainer = $('#imgContainer');

        var formData = new FormData();
        formData.append('file', jQuery('input[type=file]')[0].files[0]); 

        $('#btnSubmit').click(function() {
            var filename = $.trim(file.val());

            if (!(isJpg(filename) || isPng(filename))) {
                alert('Please browse a JPG/PNG file to upload ...');
                return;
            }

            $.ajax({
                url: "http://localhost:8080/EmployeeOnlineRegistrationForm/echofile",
                type: "POST",
                //data: formData,
                data: new FormData(document.getElementById("fileForm")),
                enctype: 'multipart/form-data',
                processData: false,
                modelAttribute:'uploadedFile',
                contentType: false,
                success: function(data){
                    imgContainer.html('');
                    //var obj = JSON.parse(response);
                    var img = '<img src="data:' + data.contenttype + ';base64,'
                    + data.base64 + '"/>';

                    alert("success");
                    imgContainer.append(img);
                },

                error: function(){                      
                    alert('Error while request..');
                }
            });
        });
    });

但当我输入图像时,ajax会提醒我成功,但图像不会显示在div中,而且我已经给出了将图像保存在该文件夹(特定目录)上的路径,但图像不会保存在该文件夹中。我该怎么办

Inspects shows the error


共 (0) 个答案