Saving the File Path in the Bank

0

I'm having a question I'd like to know with saving the path of images and files I'm uploading currently I save those files to a folder but I'd also like to have the path in the database how can I do this.

Well, the controller I use to upload this is

 @RequestMapping(value = "/uploadMultipleFile", method = RequestMethod.POST, produces = "application/json;charset=utf8")
 @ResponseBody
public MessageBean uploadMultipleFileHandler(@RequestParam("file") MultipartFile[] files) throws IOException {

    MessageBean msg = new MessageBean();
    ArrayList<Integer> arr = new ArrayList<>();

    for (int i = 0; i < files.length; i++) {
        MultipartFile file = files[i];

        if (!file.isEmpty()) {
            InputStream in = null;
            OutputStream out = null;

            try {
                String rootPath = System.getProperty("catalina.home");
                File dir = new File(rootPath + File.separator + "ArquivosItos");
                if (!dir.exists())
                    dir.mkdirs();
                File serverFile = new File(dir.getAbsolutePath() + File.separator + file.getOriginalFilename());
                in = file.getInputStream();
                out = new FileOutputStream(serverFile);
                byte[] b = new byte[1024];
                int len = 0;
                while ((len = in.read(b)) > 0) {
                    out.write(b, 0, len);
                }
                out.close();
                in.close();
                LOGGER.info("Server File Location=" + serverFile.getAbsolutePath());

            } catch (Exception e) {
                arr.add(i);
            } finally {
                if (out != null) {
                    out.close();
                    out = null;
                }

                if (in != null) {
                    in.close();
                    in = null;
                }
            }
        } else {
            arr.add(i);
        }

    }

    if (arr.size() > 0) {
        msg.setStatus(Status.CONFLICT);
        msg.setError("Files upload fail");
        msg.setErrorKys(arr);
    } else {
        msg.setStatus(Status.CREATED);
        msg.setStatusMsg("Files upload success");
    }
    return msg;
}

and the jsp being used to call the method is this

<body>

<div class="col-md-12">

    <div class="box box-info">

        <h1>
            <center>Upload Das Itos</center>
        </h1>
        <div class="container">
            <form enctype="multipart/form-data">
                <input id="file-0a" class="file" name="file" type="file" multiple
                    data-min-file-count="2"> <br>
            </form>
        </div>
    </div>
</div>  

   </table> 
        <div class="push"></div>


        <script type="text/javascript">
            var csrfParameter = '${_csrf.parameterName}';
            var csrfToken = '${_csrf.token}';
            var jsonParams = {};
            jsonParams['parentId'] = 1;
            jsonParams[csrfParameter] = csrfToken;

            $('#file-0a').fileinput(
                    {
                        uploadUrl : '/sic/ito/uploadMultipleFile',
                        extra : jsonParams,
                        allowedPreviewTypes : [ 'image', 'html', 'text',
                                'video', 'audio', 'flash' ]
                    });
            $('#file-0a')
                    .on(
                            'fileuploaderror',
                            function(event, data, previewId, index) {
                                var form = data.form, files = data.files, extra = data.extra, response = data.response, reader = data.reader;
                                console.log(data);
                                console.log('File upload error');
                            });
            $('#file-0a').on('fileerror', function(event, data) {
                console.log(data.id);
                console.log(data.index);
                console.log(data.file);
                console.log(data.reader);
                console.log(data.files);
            });
            $('#file-0a')
                    .on(
                            'fileuploaded',
                            function(event, data, previewId, index) {
                                var form = data.form, files = data.files, extra = data.extra, response = data.response, reader = data.reader;
                                console.log('File uploaded triggered');
                            });
        </script>

Just to clarify this code is uploading ok the only doubt is how to write the file path in the bank.

    
asked by anonymous 07.06.2017 / 15:22

1 answer

0

Hello, I believe you can create an object related to the file you uploaded and that represents an entity in the database. And in the database table there will be a link field. There instead of LOGGER.info ("Server File Location=" + serverFile.getAbsolutePath ()); for example you put the object.setLink (serverFile.getAbsolutePath ()); and saves the object to the bank.

    
07.06.2017 / 18:03