Wednesday, January 26, 2022
How to make spring controller Get request on controller
Tuesday, January 25, 2022
How to configure Spring Boot Jdbc template
Saturday, January 22, 2022
How to make testing cases using Selenium scripts
How to upload Multiple Image From jsp page .
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
}
How to Implement Spring security Method in Spring Boot
step 1. open dilsecodie.com .
step 2 . Make config class in your spring boot project .
step 3: Make class Security Config extends WebSecurityConfigurerAdapter
step 4 ; check spring security dependency
step 5 : copy code from dilsecodie https://dilsecodie.com/sourcecode/topic/Spring- Boot/tutorial/Spring- security
step 6: paste in your class checks all pages your need in security.
step 7: check your security will run, or you may customize according to your project
step 8 : still have an issue then ask doubt in ask question, section get an answer from an expert.
lets me example you for spring security its core part of spring which can be implemented by using some import packages :
import org.spring framework.boot.autoconfigure.EnableAutoConfiguration; - for enable autoconfig
using spring in it ,
security.crypto.bcrypt.BCryptPasswordEncoder; is used to decrypt the password submitted by the user end.
@EnableWebSecurity this annotation is used to enable auto security feature in your security class when using embedded. packages.
SecurityConfig extends WebSecurityConfigurerAdapter if suppose your security config is your class then its extends Web security method in your security class,
protected void configure(HttpSecurity HTTP) using this method in your security class provide you HTTP method with so to develop security method for it.
then in your security class use http.csrf().disable().authorizeRequests() - for .antMatchers( ) pass all the endpoints which you need to secure , so that these URL cant be access without login auth.
.antMatchers(adminService.getAllAccess().toString()).authenticated() - this line most important bcoz when we use its provide all access auth case .
when any user login then request first handled by spring security like .successHandler(customSuccessHandler) - these methods use customsuccesshandler class to valid-user, if so then only sessionid will be provided to that request.
if any request failed to authenticate then that case will be .failureHandler(customFailureHanler) - which means the custom failure handler class will provide an invalid token and a custom message to request..
But when we implement spring security in our project then any URL cant be accessed from any request it needed cross from spring security which means then how to access the index page to start the application in that case we use .loginPage(Constants.INDEX_PAGE).permitAll() - this method provides all permit to index page without any validation process.
Now will talk about success part after that session will be provided for that user., but we need to check every request having valid session or not that an be done by using invalid session URL method for ex :invalidSessionUrl(Constants.SESSION_EXPIRED).sessionFixation().migrateSession().and().httpBasi()
but we need to implement .deleteCookies("JSESSIONID") - delete cookies method to delete jsessionid also after any invalid session or session expire .