简体   繁体   中英

base64 to image file

I am using summernote and want server upload capability asynchronoustly. I am planning to convert image to base64 and send to servlet through ajax there i will save compress the file and return url of image file to editor src as below:

var edit = function() {
  $('.click2edit').summernote({
      focus: true,
      onImageUpload: function(files, editor, welEditable) {
            sendFile(files[0],editor,welEditable);
        }
      });
};
function sendFile(file,editor,welEditable) {

    alert(file.size);
    var reader = new FileReader();
    var imgfile = reader.readAsBinaryString(file);
    alert(file);
    $.ajax({
            method:"POST",
            url: 'imageupload',
            data: {imageFile:imgfile},
            success:function(response)
            {
                alert("file uploaded successfully");
                return response;
                },
            error: function(response,status,err)
            {
                alert("upload failed"); 
            }
        });
}

below is my servlet code. here I am getting file as null. I believe I should get file as string here. can somebody help why?

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("image upload");
        String file = (String)request.getParameter("imageFile");
        System.out.println("file: " + file);
        PrintWriter out = response.getWriter();
        response.setContentType("text/html");
        out.print(file);


    }

I finally found answer to this Question. I had to modify summernote and wrapped file input in form and returned the form from there which I posted using iframe.

I googled and found we can't set file name dynamically to file input because of security reasons.

你可以尝试request.getReader()吗?

Try this

public static BufferedImage decodeToImage(String imageString)
{
    BufferedImage image = null;
    byte[] imageByte;
    try
    {
        BASE64Decoder decoder = new BASE64Decoder();
        imageByte = decoder.decodeBuffer(imageString);
        ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
        image = ImageIO.read(bis);
        bis.close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    return image;
}

public static String encodeToString(BufferedImage image, String type)
{
    String imageString = null;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try
    {
        ImageIO.write(image, type, bos);
        byte[] imageBytes = bos.toByteArray();

        BASE64Encoder encoder = new BASE64Encoder();
        imageString = encoder.encode(imageBytes);

        bos.close();
    } 
    catch (IOException e)
    {
        e.printStackTrace();
    }
    return imageString;
}

If u reading file from URL

try 
{
    URL url = new URL("example.com/example.txt");
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    String str;
    while ((str = in.readLine()) != null)
    {
        // str is one line of text; readLine() strips the newline character(s)
    }
    in.close();
} 
catch (Exception e) 
{
e.printStackTrace();
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM