2

I have created a java program, my program needs an image at some point to be written into Back end database(MySQL).

after this Question i know that i can't read files from deployed jar files, So therefore t tried this

package sample;


import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {


    public static void main(String[] args) throws IOException {


        File file = new File("11.jpg");



        byte[] allBytes=Files.readAllBytes(file.toPath());
         Path path = Paths.get("0.txt");

         Files.write(path, allBytes);

    }

}

where 11.jpg is the image file that i want to use. in the above code i converted 11.jpg to 0.txt then i copied all from the 0 text and stored inside a String and did this

package sample;


import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {


    public static void main(String[] args) throws IOException {


        File file = new File("11.jpg");





        String s = "���� \u0010JFIF \u0001\u0001\u0001 ` `  �� C \u0002\u0001\u0001\u0002\u0001\u0001\u0002\u0002\u0002\u0002\u0002\u0002\u0002\u0002\u0003\u0005\u0003\u0003\u0003\u0003\u0003\u0006\u0004\u0004\u0003\u0005\u0007\u0006\u0007\u0007\u0007\u0006\u0007\u0007\b\t\u000B\t\b\b\n" +
                "\b\u0007\u0007\n" +
                "\n" +
                "\n" +
                "\u000B\f\f\f\f\u0007\t\u000E\u000F\n" +
                "\f\u000E\u000B\f\f\f�� C\u0001\u0002\u0002\u0002\u0003\u0003\u0003\u0006\u0003\u0003\u0006\f\b\u0007\b\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f\f�� \u0011\b c D\u0003\u0001\" \u0002\u0011\u0001\u0003\u0011\u0001�� \u001F  \u0001\u0005\u0001\u0001\u0001\u0001\u0001\u0001        \u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n" +
                "\u000B�� �\u0010 \u0002\u0001\u0003\u0003\u0002\u0004\u0003\u0005\u0005\u0004\u0004  \u0001}\u0001\u0002\u0003 \u0004\u0011\u0005\u0012!1A\u0006\u0013Qa\u0007\"q\u00142���\b#B��\u0015R��$3br�\t\n" +
                "\u0016\u0017\u0018\u0019\u001A%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������� \u001F\u0001 \u0003\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001      \u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n" +
                "\u000B�� �\u0011 \u0002\u0001\u0002\u0004\u0004\u0003\u0004\u0007\u0005\u0004\u0004 \u0001\u0002w \u0001\u0002\u0003\u0011\u0004\u0005!1\u0006\u0012AQ\u0007aq\u0013\"2�\b\u0014B����\t#3R�\u0015br�\n" +
                "\u0016$4�%�\u0017\u0018\u0019\u001A&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz�������������������������������������������������������������������������� \f\u0003\u0001 \u0002\u0011\u0003\u0011 ? ���'\u0014?J��7���� d��7��h�ώ|sug4�\u001F�n�cu�j�Ӧ\u0001s\n" +
                "�3�\u0015�3'�s�2�\u0018p���� �ѥ:����n�糱�M|����\n" +
                "�ῂ\u001F\u0014�� \n" +
                "�\u000B�9�H��!��\u001A��|$'��9��o\u001E\t�\u000B���ċl�#\u0005v\u000B�\"\u0012$;+ʼ\u0013�_|qӼO�\n" +
                "k��2�\u001D�H�\u0010\u0012���\f���Ҧ����H�{ˉ��yhn\u0007��;If��['���_\u0018��?\u000F\u0013�\t��\u000F�_�5�|?���� �\u0017���\u0002�kگ�F�y\u001D������\tn �-�7@Nɽق�4j��\u0017'JO��d�v>�� ��ǩ궶�� ���\u0005+�x���K(����\u0015�\u0011I4�Y\u007F��E\u000F�\u0014�M!Xϖ@l�\u001F��gߏ^\u0017��>\n" +
                "�g�\u001F��H��\t���=GM�E*^7�\u0016S�:�dt<�+)�\u001A��\u05FFm-S���w��'�o\n" +
                "������xcF>!����(\u001A=���Y!�\u001Aŭέ�E\u000B��qYI\u001C� ��$�)x\u000BF=��B��5\u007F\u000F�>�\u007F��\u0011�� \u000E�[�\u0013I�\u000Bx��\u000E�m�\u001D\u001E���f��pB�#Ms5ݬ� [W�\b�r2ۏ67��zb�x�?ݫK�\u0005S��:q@�����g�\u0014o�%�|7�������g��\u007F��S��rA�\u0019[c�\u001Dr;_���\f�������n�\u001B�5�ˎI�����S����h�-�#Ya�'\u000F\u001C��*�Â\b �:��q\u0018J�mδ{3X�2�\u0014Q\\�\u00197\u000B_���Q�گ�w��� \u0005]����\u0005����3��\u000Fj���s�� d��{��,j��I�Z\u000B\t\u001AI�e���5hR8Oْf���dW�?�P����_\u0012�\u001C�\u007F\u0012|k��^\u0019o��s�]kJ�������\u00135�m\u0005�3[ʌ�����\u0004���W���cJ���-\b��?8�\u000E|\u001A�t�wV�>\u001D�xvx~\tk�\u0016o� ��������Z��|S>��N�gm �I?�ac\n" +
                "ٺ_3E�E���L�g��>#i� ��M�\u001F\u0011|\u001E�ρ|Q�/���.4�*�Y�ִ����Q��ƷX\u001D'���ċk#}��7h�_�U�q������~4����-��>\u001B����n.'�O\u0017x�K�\u0010���\u0001�G\u0017�v�}x�i!�;��e\u0003I\u001Dő>M/��)~ѿ�5=o�^\u000B��\u000B��w�:��\u0015���_\u0006xgQ_��\u000B+�9m$��Gi��!\u0017U�q�;ic���I��ڴ�g:=�ǿ�W�|s���\t~�\u007F\u0013>3k^%�\u0001�4\u007F\u0006��y��5����G�]�]�V��6V����md\ff(Q�{w�&\u001C� �)�OƚN����\u000E��}\u007Fź\\�߆�O\fh��\u0016z��o\"�\n" +
                "ƛ\u000E�\u0015�s\u0018Y//淲�k��e\u000F{\f04�\u000F�A?�/�^\u0006�< �\u001Cu�&���\u000EO���:\u0005����)[U�\u001BK�� ���m`{[��lEutd�4�\n" +
                "��VF\u0016�� gu�\u001E��#X�sJ��o��\u001C�:��y��]sA��>��� \u0014iq}�\u000F��N1�\u0012X\\Gn��\tg�^�������ь��\u007Fj\u001D?�o�~1h>#�|kq�����>\u001Dּm�kcD}\u000F������7���\u0018b��Ӧ���cz�t�y\u0015�ğ���E�o�:��\u0013��k������-\u0017E���j'}���A�&�3�<�&�[�x���\u0005�-������� \u0004����3�Oڻ�\u001F\u0003|K�x�W�+�h-�q���5m�4O\u001D��H��{[�WI\u0004ws[�i��-��s=���p�]ϐ���y�8ǰ\u001D\u0007�xy�x�\n" +
                "�Z�Jk[�QE\u0015��\\dW�~ڿ\u0005o�h�������1�MS��\u000Eմ+\u0016��\u001A�\\��\u0014E�����\u0018�Pl�_��Oz�S�u\u0014�@z��о����3K�<Iy�j:M�~(����Z��~��\u0019���+�\u0007FK�\u001A;Խ�U�U��u�^\\\u000Bp&VH\u007F���'wg�iZ^��9u�\n" +
                "O�\u007F\u000FxsU�b�w��_�3��\u001A\u000E�`ڝ��W\u0003X��9<7l�ͫIm\u0006����n��H�@�o��&\u001F��\"� �Z�\u001Eh�����W�\u0013����}\n" +
                "����mcR�l��#B�~�5��S:I\u0019�.%\u000F\f�����>��\t�\u0017����\u0003J���?\u0019x\n" +
                "?��\u0014�S��\u0018I,�'�Yj��\u001B�,%��;\u0017\\���<�,2�\u0014�w�\u0018��G\u0011\n" +
                "�S]�r٭\f�\u001Ax���zׅg�$]3]�>6h�#\uD896\uDFC7��e��:�\n" +
                "Eo.\u001B̝��i����$q]ޠ��&��K�\\x��7�\u001E���\u001A\u0006�{���Z\n" +
                "��t���R��w�߅t�(�c��kv�����k �X[�\n" +
                "Ω���e�$~��\u007F�\u001B�\u0003��#��\u0002�Ao�D>\u001C���L�� ŚDV��(�p�:����\u001B�V���\u0003M�m�!�Y.\f��\\���8�&� �W��|\u000B���\u000F\u0002�<C-��M?�\u0016�.��\\�X���7Z�����\u0012[�|�U�x�Z\u000B�gY��&\u0001�T#�tl\u000Fq� �\u001Ex'F���oC�\f��\u007F��<\u000F��F_\u0017x>��y~\u0007�\n" +
                "��$0�֗BI\":d�Ũ^C\u0005��=���!�ID��\u000E!_+\u007F�\u0017> Z�\u000B� �q|/�}OS�\u0017�<\u007F��7��\"ֵ+�on�\u001B��\u001Bs\u001A\u0019efc\u0015����B3��k\u0010�~�Q�_\u001D�b=�!���ޚ�E��+�(3H�\u0003�\n" +
                "T�u�\u000F\fh����woa�i��]�]\\H#��\u0018Ի��xUU\u0004�x U�.MF=@�c���c����\n" +
                "3�\u001Cx�{x���!�����|�cd�\u001E�'X�� ��}��\f �\b���|5�|x����\u007F��ÿ��zG����v�\u001D�]/�Zj~)��\tiw\"�\f�v\u05F7S۴��\\Z���Fk�\u007Fg�۳����~4�ڧĚg�O�O��\u001A���[�~\u0014�e��\u0006���]��X�jJ�ˆ6��Y��y����!\n" +
                "��\u0003���_�_�����\u007F�\u007F\u001E�\u0003��>$�o��!�O\u000B�xz\u0013���Z����ŻO\u001C&{��k�� ��O��\u0012�$.B}�\u001E���\u001ArwiX����\u001D����\u0014G�'�\u0006��ڞ���\u001E$մ�W�zy�M��kW�n��\u001C�,��V�s��7\"��؇�����/�o�Z��o����_\u0015�\u0005o�K���]{G��>��ov��./<;y4��A�_�ev��~1��oس�a�'�x[_�<{�V�i�<�g��]�K�\u0019�6�\u0011�ty\u0005���L�p�\\[�ah\f�\u0014�������٫�W�/���P\u001F\u0005�]�>\u0004��\u001A���\u0012h��\u0007�,��\u0004j\u0013���\u0017s�������\u0007;<@\u007F�\u0017�d�\u0019U��`�\u0016��\u0016ӌ�{\n" +
                "���\u0011gk�~�?\u0005 ���,`�\u000E�\u001D���\f��: �T�\t\u00038'���ٯ�?�'/�\u0014��\u0007�� ������\u0018�0��\u0007��tO�O\u000Fx��?���o|\n" +
                "�ޟ\u001BʱGm1kT�ѣڲ<��z�3G_�(�<���_\u001D�a'F���֦�i�J(����a�ҾG� ��x��~\u0002|4�v���h� \u0014�1�#�ڝ�ɾ=R�}n�[�Y\u0001��$0H���B�r\u0018���W�~�_����H~���\u0014�д�\u0018����\u000E]k0<�\t��:���\u0004syg�X�6�\u0017�72$��\\��ۗN0��S�2gw\u001D\u000F�?���������'o���;��_\u0002j\u001E.�W�k>\u001F�Ӯ\u0379��\u0016�\u001E�}�\u07FCW�ŵ.���/c\u0012\u0010�� �#IY�)� \u00051����m��\u000B@о\u001E��#���TAk\u0016��m?Qy�P����\u0005��-�['�,�\n" +
                "�\u0013�(�\u0004_�~\u000F��_\u001E�fOۿ�o��(�G㯅4O\u0004x\u000Ey4O\f�#ź����:�Dڽ��xr\u000B��{;�B��R\u0006��\f�@\\F���\u000F�L��_����7�|+�-[Hִ�C�ڦ��i�?Ҭ��>{�I�ZY[�H��ɬ`ho�R��\u0012��,\u001F��\u0007A���;��\u000Bk\u001Ew�|\u0006�e�'�<\u0003� \b��m\u001FH�_�F�IӴ�m;Ğ\u001C\u00105�+�\u000B�^�\uD956\uDF64�q5�Q9�����\u000F��\u007F��~��|\u0019�M�\u007F\u000Ex\u0013�~4���C�\u0016\u001E\u001A����n�5��t��k�u�S\f�a�]j�p�4�p叝]\u0017�|-��\u001E\u000F������?ŚE�\u0006'�Ğ\u001A����z�Z����:Y��ݶ��H��A�@n�k���?�� ���� վ\u0019�\u0014鷚\u0016��-\u0013L��ݗ�<+�\u0012\u001F@����w\u001B\u0016�%�\u0019'2\u0012Q��B�\u0006D�\u0014�֤�\u07B74� o\u000F�!N��\u0011�\u0002��|q�|G�-���7��9�1�� �O,���Ӧ�-bK�c�(�c-�f�����`w#\u0001�Y� \u0004�����y��>\u000Bx��!�Ğ-�6���2\u0004��f��Ie\n" +
                "8P����\u0018\n" +
                "�[��� `\u000F���Y\u000BO\fx\u001B�\u001E1��\u007F\u0007�g��់���բ�<q �L�&�����+�Nl��ت����\u0013\b�\u000F�_\n" +
                "xoO�w�t�\u001FJ���K�-b����|��D�#�}�T\u0001��\u001F;�\u001Fe\u001ASw�ˤ��i\n" +
                "(��`�l��B��\u000E\u0006���U\u07B5�k� \u0005G� �;��\u001E�6�a�W�|\u0005�gS��?\u000F���|9���]R\u001B�\u001E��&?l�[�w\u0006x\u001A\u001B�b�r$��|����p��MƬ�Irkc�[�W�������m>#|;�G�\u000B]-�[(|K�Z��f��\u0018�G\b[j�3�zW�i߰\u000F칣|}��D?����1�˦ǩ�j֞\u0011�O�upY��Auo��DAC4.\u0011�0t\u000E��%��\u001F�Bmw�v>.�<\u0017�� ���\n" +
                "��4�\fj>\u0016�φ�E����-\u000E����C����Z]��]�C��\u0005�L�\n" +
                "���?����7��\u0003�m��~��{{�\u000E�:��� \b���:]�\u001E\u0014�O��T� ���j;f{Xa�����@Ƀ�C\u000BB\u0011�U\u074Cy��>���\t��Z���\u0004��\u007Fv=J�\u0014\u007F�D��*�?�N��JU�ᗆo=>�\u000B\\���3f�E� �`� �)|a� ��w�#�ς�\u0014��'È�\u0018��\u000EA���Ԛ���\u00184�8�%�n�-�\\,Q�c��mA���\b�u��_�:�����R|7�<Y�̶\u001A\u0007��?du�?�B��I\u007Fl�BN<�Y�F�-A��\t<�ehc�S����w��ױ���|7�\"|\u0014է�g��c�4�7Ro\u000Fx/E�]^�#�+�l�5�f�f�sm8�p\u000F[�� \u0016Xx�ºv��\\5Ι���6�/\u000B20�\u001A7\u0001чFG\u0001��\u0018\u0002\b���\u0003�\b_�\u000F\u0004x�k=O�o��N�L\uD863\uDDCF�\u001A7�|?�j:������ R�i��L:u�ܚ\\���Py\u0013�l���À\u001A��� �[��?��\u007F�~���4\u001B-\u0016y|{�S͂����{Y���ay,v1�n��\u000Bf6$Q��\u00121\u001Cj��͎�a�G�F�4�\u0019J����qE-\u0015�\u001A��g���� |�}�?���Ȣ��O�R�U�\u001C\u0017a�h� �\u007F�\"���� <���\u0014QG�����\u000B��,_��?�G�b� �q� �\"�(�\u0014��}�v\u0006�3� ,���\u0014}�/���Ȣ�>�K�W�\u0017b��?�'��E\u0014Q�)\u007F*�����";


        byte[] allBytes=s.getBytes()


         Path path = Paths.get("11.jpg");

         Files.write(path, allBytes);

    }

}

but the image file i got is unreadable!

the image is only about 1.3 kb.

is it possible what i am trying to do?

user55924
  • 142
  • 16
  • 3
    Why you are trying to read it as a string? Just use FileInputStream to read the flie and write as a blob to the database. – kk. Jun 06 '17 at 09:26
  • 3
    It is possible if you store the data in a ``byte[]``. A ``String`` is supposed to be a human-readable representation of text data, which will not work for non-utf8 binary data. – f1sh Jun 06 '17 at 09:26
  • Strings are not byte arrays, and are not suitable for storing arbitrary bytes. – Jesper Jun 06 '17 at 09:26
  • 3
    *"after this Question i know that i can't read files from deployed jar files"* this is not correct. You can store your images in the jar file and read them from there. – Tom Jun 06 '17 at 09:27
  • i am not trying it to read it as a string @KrishnaKuntala, I want the data that i can store in some variable and when needed convert it into a image file! – user55924 Jun 06 '17 at 09:28
  • 2
    Why not byte array and then store it as BLOB in database? – gonczor Jun 06 '17 at 09:29
  • @Tom sorry tom but i am getting error while reading file from inside of a jar file java.io.FileNotFoundException – user55924 Jun 06 '17 at 09:36
  • 3
    There are __many__ questions about reading an image from a jar file. Search for them to fix that issue. – Tom Jun 06 '17 at 09:37
  • @f1sh are you trying to say Writing byte[] s instead of String s will work? but in last line of my code i am doing this byte[] allBytes=s.getBytes(); – user55924 Jun 06 '17 at 09:39

2 Answers2

4

Although it is a bad idea, you can carry an image in the string variable.

To do this you need to convert your image to Base64. E.g. you can use recommendations given in post How to convert an Image to base64 string in java?

Then you can use carry your image inside a string variable and save it to the DB in the text-based column.

But please consider other options like:

  • Storing binary data in BLOB format
  • Putting your image to your jar archive (Many asset files can be stored within "/resource" folder of you project. If you use maven please consider this option.
nikita_pavlenko
  • 658
  • 3
  • 11
1

The solution is very simple, the JVM is capable of bundling all your image files in the jar files. Simply create resource folder in your project directory and place the image you need to store, save the url in the database for other transactions after deployment, BLOB would work but it has a performance downside.

Samuel Owino
  • 747
  • 1
  • 12
  • 24