0
I am having a doubt I would like to know with saving the path of images and files that I currently upload I save this files in a folder but I would also like to have the path in the database how can I do this.
well the controller I use to upload is this
@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 database.
opa blz so I did the way you said it saves the upload but it does not save the path in the bank would have any more idea
– Wesley Santos
Hello, it was bad take this time without answering, it was some time that I logged here, I hope you have already found the answer searching through the net, but if the doubt persists ... how do you access the database, query, record and such? in the code you posted, I didn’t notice how you access the bank, so I gave a somewhat generic response
– Ari Clemente
Opa face already solved this issue more thank you for the attention
– Wesley Santos