Upload Angularjs image with Java server

Asked

Viewed 898 times

2

I’m having a problem uploading a web image, I’m using the angular file upload, when I upload it saves the image in the folder Frame -> Images of the browser, and when I will read the inputStream there on my server it does not return the image. How to solve the problem?

  • Cleiton, welcome to Stack Overflow. I think you need to elaborate a little better on your question. I don’t see how I can help but make innumerable conjectures. For example, you said the image is saved, but if you open it in an editor it is normal? Is the file the same size as the original? Are there any messages on the console? How is the code where you open Inputstream?

  • Thank you. Then the case and the following in the client the image appears but I need to take the image to record on the server, but the problem is that I can’t get the image on the server, I can get the image name, size, but the problem is to take for example the byte image and convert to image on the server... It became clearer my doubt ?

  • It helped a little, but understand that there are many different ways to recover a Java upload (Spring, Apache Commons File Upload, Primefaces, etc). It would be good for you to post the code of the method where you do this.

  • I’m using Commons, there goes the code if (req.getHeader("Content-Type") != null
 && req.getHeader("Content-Type").startsWith(
 "multipart/form-data")) {
 ServletFileUpload upload = new ServletFileUpload();
 FileItemIterator iterator = upload.getItemIterator(req); there I use while (iterator.hasNext()) { FileItemStream item = iterator.next(); was trying to get the image through the inputStream so : if (item.getName() != null) {

 File uploadedFile = new File(path + "/" + item.getName());

  • This is the part where I try to catch the inputStream and turn into an image byte[] bytes = new byte[2048];

 OutputStream outpuStream = new FileOutputStream(
 new File(uploadedFile.getAbsolutePath()));

 System.out.println(uploadedFile.getAbsolutePath());
 int read = 0;

 System.out.println(req.getInputStream().toString());
 
 while ((read = req.getInputStream().read(bytes)) != -1) {
 
 outpuStream.write(bytes, 0, read);

} outpuStream.flush(); outpuStream.close();

  • 1

    @Please make an edit on your question to put the code of these comments. It is confusing to put as a comment. Then delete them. Here is different from a forum.

Show 1 more comment

1 answer

1

I believe there is a misconception in your code. First you retrieve an item like this:

FileItemStream item = iterator.next();

Then, while reading the bytes of the file, you recover the InputStream general request:

while ((read = req.getInputStream().read(bytes)) != -1) { ... }

However, the code should retrieve the InputStream of the item, thus:

InputStream inputStream = item.openStream();
while ((read = inputStream.read(bytes)) != -1) { ... }

After all, the request could have more fields and files, it’s not even?

Anyway, I refactored a little the code you posted in the comments and I came to this:

if (ServletFileUpload.isMultipartContent(req)) {

    FileItemFactory fileItemFactory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(fileItemFactory);
    FileItemIterator iterator = upload.getItemIterator(req);
    while (iterator.hasNext()) {

        FileItemStream item = iterator.next();
        if (item.getName() != null) {

            File uploadedFile = new File(path + "/" + item.getName());

            byte[] bytes = new byte[2048];
            int read = 0;
            OutputStream outpuStream = new FileOutputStream(new File(uploadedFile.getAbsolutePath()));
            InputStream inputStream = item.openStream();
            while ((read = inputStream.read(bytes)) != -1) {
                outpuStream.write(bytes, 0, read);
            }
            outpuStream.close();
        }

    }

}
  • Really that call of inputStream is wrong, I was testing and I forgot but with the changes the mistakes remain the same. SEVERE: Servlet.service() for servlet [file_upload] in context with path [/UploadImagem] threw exception [org.apache.commons.fileupload.FileItemStream$ItemSkippedException] with root cause
org.apache.commons.fileupload.FileItemStream$ItemSkippedException
 at org.apache.Commons.fileupload.Fileuploadbase$Fileitemiteratorimpl$Fileitemstreamimpl.openStream(Fileuploadbase.java:855) Any suggestions ?

  • In my view the inputStream is not returning the image

  • ItemSkippedException is launched when you try to access the InputStream after calling the ext to the next Stream. By chance you are listing the items to afterward try to read the bytes?

  • no, already trying to read the bytes direct

Browser other questions tagged

You are not signed in. Login or sign up in order to post.