pass byte[] , file parameter to a method java

I m confused to how to pass byte array and file to a method in Java : when I call enregistre method : enregistre(img3File, file3) and enregistre(img4File, file4) enregistre(img5File, file5) , it doesn t upload the file to the database, but when I call the part of program without method( I repeat the code with :img2File and file2 ) it worked, I dont want to repeat the code 5 times, I beleive there are a solution , help me to figure out what is wrong when I call the method please.

@RequestMapping(value="/save",method=RequestMethod.POST)
public String add (
               @RequestParam("photos") MultipartFile file,
               @RequestParam("photos2") MultipartFile file2,
               @RequestParam("photos3") MultipartFile file3,
               @RequestParam("photos4") MultipartFile file4,
               @RequestParam("photos5") MultipartFile file5)


    {
               byte[] img1File=null;

                try {
//////////////////////// part1
                     img1File= file.getBytes();
                     BufferedOutputStream stream;
                     stream = new BufferedOutputStream(new FileOutputStream(new File("ben1")));

                     stream.write(img1File);
                     stream.close();
//////////////////// end part 1 
                } catch (FileNotFoundException e) {

                    e.printStackTrace();
                } catch (IOException e) {

                    e.printStackTrace();
                }

                 // here I repeat the code and I don t want to repeat it    
                byte[] img2File=null;

                try {
////////////////////////  part 2 
                     img2File= file2.getBytes();
                     BufferedOutputStream stream;
                     stream = new BufferedOutputStream(new FileOutputStream(new File("ben1")));

                     stream.write(img2File);
                     stream.close();
////////////////////////end   part 2 

                } catch (FileNotFoundException e) {

                    e.printStackTrace();
                } catch (IOException e) {

                    e.printStackTrace();
                }

                byte[] img3File=null;
                enregistre(img3File, file3);

                byte[] img4File=null;
                enregistre(img4File, file4);

                byte[] img5File=null;
                enregistre(img5File, file5);

        Annonce annonce=new Annonce();

        annonce.setPhotos(img1File);
        annonce.setPhotos2(img2File);
        annonce.setPhotos3(img3File);
        annonce.setPhotos4(img4File);
        annonce.setPhotos5(img5File);

        annoncedao.save(annonce);
        return "SuccessAddAnnonce";

    // method
  public void enregistre(byte[] imgFile,MultipartFile file)
    {


        try {
            imgFile= file.getBytes();

             BufferedOutputStream stream;
             stream = new BufferedOutputStream(new FileOutputStream(new File("ben1")));

             stream.write(imgFile);
             stream.close();
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        } 

    }
Jon Skeet
people
quotationmark

Look at what your enregistre method does:

BufferedOutputStream stream;
stream = new BufferedOutputStream(new FileOutputStream(new File("ben1")));
stream.write(imgFile);
stream.close();

If you call that method multiple times, it will write to the same file multiple times. There's no point in that - you'll just end up with the file containing the data for the last call. Note that the method doesn't do anything with a database - it just writes a file. You haven't explained to us where the code that does write to the database is, but presumably it's reading from that file at some point.

(Aside from that, the first parameter of the method is completely useless, as you immediately reassign it. This code has some serious issues in terms of code hygiene, but I've focused just on what you were asking.)

people

See more on this question at Stackoverflow