The servlet has this code:

try { List<FileItem> multiparts = new ServletFileUpload( new DiskFileItemFactory()).parseRequest(request); BufferedImage image = null; byte[] imageByte; for(FileItem item : multiparts){ if(!item.isFormField()){ String name = new File(item.getName()).getName(); item.write( new File("C:\\temp" + File.separator + name)); BASE64Decoder decoder = new BASE64Decoder(); imageByte = decoder.decodeBuffer(File.separator + name); ByteArrayInputStream bis = new ByteArrayInputStream(imageByte); image = ImageIO.read(bis); System.out.println(Arrays.toString(imageByte)); System.out.println(image); bis.close(); } } } catch (Exception ignore) { } 

I'm trying to encode image base64 but my image why it is null , what am I doing wrong?

 { id: "upl1", view: "uploader", icon: "upload", type: "iconButton", label: "Загрузить", width: 110, align: 'center', multiple: false, link: "mylist", formData: {}, on: { onBeforeFileAdd: function (item) { this.define('formData', {fileName: item.name}); var type = item.type.toLowerCase(); if (type != "png" && type != "jpg"){ messageBox('Ошибка', "Только png или jpg файлы"); return false; } }, onAfterFileAdd: function (item) { }, onUploadComplete: function (response) { // uploadComplete(response); } }, upload: "/labMarket/import" } 

Here is the client code. The servlet sees the picture, test - saves to the specified folder. But decode fails.

  • 3
    String decode = Base64 .getEncoder() .encodeToString( (name) .getBytes() ); try it like this - Senior Pomidor
  • Expand previous comment decoder - decode / decrypt encoder - encode / encrypt - Peter Slusar
  • one
    ... and just in case I will clarify. You know that using this encoder you increase the size of the transmitted file by a third? - Peter Slusar
  • No, I need to keep a profile in the database of the byte which encoder to use will be better I don’t know yet - Aslan Kussein
  • one
    @AslanKussein if everything is like that, then the only question remains in the library for me personally most often to use Base64 - Apache Commons - Peter Slusar

0