step 1 : open dilsecodie
step 2: copy code in controller from spring boot section to upload multiple images from UI
step3: Map MultipartFile from the JSP page.
step 4: form submit from JSP to the controller with the same name as mentioned in the controller parameter.
When there is a problem uploading multiple images, from multiple parts sending to the controller to upload file
When there is a problem uploading multiple images,
from multiple parts sending to the controller to upload file .
suppose when we are uploading multiple images submit from form using jsp pages ,
so enctype="multipart/form-data" in form declare html tag to data sended to
multipart using method="post" so send data in post method .
but we need to declare type="file" name="myfile" id="myfile" accept="image/*" multiple="multiple"
input field with these tag in it , so doc engine aware about data .
after submitting to controller
@RequestMapping(value = "/yourenpoint", method = RequestMethod.POST)- your controller
should be like this to receive request from jsp pages .
ModelAndView getAddMultipleImagesUplaoded(HttpServletRequest request ,@RequestParam("myfile")
MultipartFile[] file ,@RequestParam("myfile2") MultipartFile[] file2 ,
@RequestParam("myfile3") MultipartFile[] file3 ,@RequestParam("myfile4")
MultipartFile[] file4 , @RequestParam("myfile5") MultipartFile[] file5 )
this example shows you how can we upload multiple images from a single input field,
as we can see right now taken only 5 fields each having multiple images data in it.
MultipartFile is a class is which provided by springframework.core package extend InputStreamSource
class to achieve the read method from it.
you don't have to make this class as you just need an object of this class in your controller.
After this now let's see how we can achieve its values , we can simply use
for (int i=1; i < 6; i++) { loop as per our requirement , in example you can see that
we need only 5 multiple parts for now.
To stop duplicate we can manage simple logic by using if(i==1)
filepath = SaveFile(file);
if(i==2)
filepath = SaveFile(file2);
if(i==3)
filepath = SaveFile(file3);
if(i==4)filepath = SaveFile(file4);
if(i==5)filepath = SaveFile(file5);
SaveFile is just a custom method to upload file in location or folder .
SaveFile(MultipartFile[] file - this method you can need to pass file
After all this read file
for(int i=0;i<file.length;i++)
{
if(file!=null && file[i].getSize()>0){
String checkfile =file[i].getOriginalFilename();
InputStream fileInputStream = null;
if(checkfile != null || checkfile != ""){
try {
fileInputStream = file[i].getInputStream();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
These are snippets of code to get file.
After this save file to location
String path = filePath + "/" + flName;
File saveFile = new File(path); try {
Files.copy(fileInputStream, saveFile.toPath(),
StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
another way can make directory also , to save file in that location
File theDir = new File(filePath);
if (!theDir.exists()) {
boolean result = false;
try{
theDir.mkdirs();
result = true; - this variable will show you uploaded status .
fileCreationStatus=false; this variable we use to check wether filecreated or not
}
No comments:
Post a Comment