2

I want to receive from client image and save it to a folder. When I receive it from POST-MAN it generates only bytes:

ˇÿˇ‡ JFIF H H ˇ· XExif MM * ái & † † † ± ˇÌ 8Photoshop 3.0 8BIM 8BIM % ‘ åŸè ≤ ÈÄ òϯB~ˇ¿ ± " ˇƒ 

It's ok, I can receive it and save it.

But when I try to recieve from IOS, it send me also some information in body:

--Boundary+71ADC8832D9A5F95
Content-Disposition: form-data; name="file"; filename="fileName.jpg"
Content-Type: image/jpeg

ˇÿˇ‡ JFIF H H ˇ· XExif MM * ái & † † † ± ˇÌ 8Photoshop 3.0 8BIM 8BIM % ‘ åŸè ≤ ÈÄ òϯB~ˇ¿ ± " ˇƒ 

How can I get from it the only image?

My Java code is:

InputStream stream = request.getInputStream();
bytes = IOUtils.toByteArray(stream);
File file = new File("myfile/1.jpg");
BufferedImage img = ImageIO.read(new ByteArrayInputStream(bytes));
ImageIO.write(img, "JPEG", file);

Updated: For Multipart upload image i added code:

if(request instanceof MultipartHttpServletRequest){
    /**
     * multipart
     */
    MultipartHttpServletRequest mrequest = (MultipartHttpServletRequest) request;
    Iterator<String> itr =  mrequest.getFileNames();
    MultipartFile file = mrequest.getFile(itr.next());
    bytes = file.getBytes();
}

But it is not an instance of MultipartHttpServletRequest. Why?

Roham Rafii
  • 2,929
  • 7
  • 35
  • 49
annoirq
  • 825
  • 5
  • 18
  • 30
  • I believe your `
    ` element is missing the `enctype='multipart/form-data'`. `` requires this, and I think IOS auto-adjusts to this, while your other browser doesn't. You should always be getting what IOS is sending, and you have to process that different data format. Spring (which you tagged) has support for processing the file upload format, so look at Spring documentation for how to use it.
    – Andreas Aug 18 '15 at 04:31
  • so, how to recieve this multipart/form-data? Yes, this is my question – annoirq Aug 18 '15 at 04:33
  • I updated my first comment, but the short answer is: Look at Spring documentation for how to handle file upload. – Andreas Aug 18 '15 at 04:35
  • please, see updated topic – annoirq Aug 18 '15 at 04:37
  • Are you using Spring MVC? Usually with Spring MVC, you don't handle the request object directly. You specify the parameters you need and Spring will map it for you. – Andreas Aug 18 '15 at 04:37
  • See https://spring.io/guides/gs/uploading-files/ – Andreas Aug 18 '15 at 04:39
  • now, it can't enter to action – annoirq Aug 18 '15 at 04:46
  • Can't see what you did, so can't help. Did you look at the examples in the link I provided? – Andreas Aug 18 '15 at 04:48
  • for html it works, it's not working getting from ios. – annoirq Aug 18 '15 at 04:48
  • problem is, that it cannot recognize @RequestParam("name") String name, @RequestParam("file") MultipartFile file parameters from this http request sent from ios – annoirq Aug 18 '15 at 04:49
  • thanks, Andreas now it works. – annoirq Aug 18 '15 at 06:38
  • Don't give the solution in your question. Answer your own question and accept that as the final answer. – Andreas Aug 18 '15 at 06:40

2 Answers2

2

I found solution:

Problem in spring and tomcat configuration.... I've added

<Context
   allowCasualMultipartParsing="true" /> 

in server.xml of tomcat.

Then added bean to WebConfig:

@Bean
public MultipartResolver multipartResolver() {
    CommonsMultipartResolver resolver = new CommonsMultipartResolver();
    resolver.setDefaultEncoding("utf-8");
    return resolver;
}
annoirq
  • 825
  • 5
  • 18
  • 30
1

Try this code. It has worked for me. Put the code in jsp page itself with <% %> tags

<%
Connection conn=null;
   Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
    conn = DriverManager.getConnection(//your connection string//);

   PreparedStatement psImageInsertDatabase=null;
   PreparedStatement psImageInsertDatabase2=null;



   byte[] b=null;
   try{

      String temp=(String)session.getAttribute("myId");
      String sqlImageInsertDatabase="insert into upload_image (bImage) values (?)";
      String sqlImageInsertDatabase2="UPDATE [dbo].[upload_image] SET [iImageID] = '"+temp+"' WHERE [iImageID] is null AND [bImage] is not null";
      psImageInsertDatabase=conn.prepareStatement(sqlImageInsertDatabase);

      DiskFileItemFactory factory = new DiskFileItemFactory();

      ServletFileUpload sfu = new ServletFileUpload(factory);
      List items = sfu.parseRequest(request);

      Iterator iter = items.iterator();

      while (iter.hasNext()) {
         FileItem item = (FileItem) iter.next();
         if (!item.isFormField()) {
              b = item.get();
          }
      }

      psImageInsertDatabase.setBytes(1,b);
      psImageInsertDatabase.executeUpdate();

      psImageInsertDatabase2=conn.prepareStatement(sqlImageInsertDatabase2);
      psImageInsertDatabase2.executeUpdate();
   }
   catch(Exception e)
   {
     e.printStackTrace();
     response.sendRedirect("addimage.jsp");
   }

%>
Nik
  • 452
  • 5
  • 17