【问题标题】:Image File Upload Through Rest WebService通过 Rest WebService 上传图片文件
【发布时间】:2018-02-22 02:16:38
【问题描述】:

我已经写了下面的代码来做到这一点-:)

                @POST
                @Path("/UploadProfileImage")
                @Consumes(MediaType.MULTIPART_FORM_DATA) 
                @Produces(MediaType.APPLICATION_JSON)
              public String uploadProfileImage(@FormDataParam("imageFile") InputStream uploadedImageInputStream,@HeaderParam("mPolicyGroupSeqId")String PolicyGroupSeqId){


        JSONArray arra = new JSONArray();           
LinkedHashMap<String, String> mapObject = new LinkedHashMap<String, String>();
 LinkedHashMap<String,Object > mapObject1 = new LinkedHashMap<String, Object>();
                    ArrayList<Object> LogoList = new ArrayList<Object>();
                    try {

                        System.out.println("upload profile image");
String strPolicyGroupSeqId=TTKCommon.checkNull(PolicyGroupSeqId);

    WebServiceManager webServiceManager=this.getWebServiceManagerObject();

    //byte[]   bytes = IOUtils.toByteArray(uploadedImageInputStream);

                     byte[] bytes = new byte[1024];
                     int bytesRead=0;
                     ByteArrayOutputStream output = new ByteArrayOutputStream();
                     while ((bytesRead = uploadedImageInputStream.read(bytes,0,bytes.length)) != -1)
                     {
                         output.write(bytes, 0, bytesRead);
                     }

                     output.flush();

                     byte[] byteArray = output.toByteArray();


                     String filePath = "D:/Download Here/exist.jpg";


                     FileOutputStream fos = new FileOutputStream(filePath);
                     BufferedOutputStream outputStream = new BufferedOutputStream(fos);
                     outputStream.write(output.toByteArray());

                     outputStream.flush();




                            if(uploadedImageInputStream != null){
                                System.out.println("inputstream is not a null value");
                            }
                            if(bytes != null){
                                System.out.println("bytes is not a null value");
                            }

                       int status=webServiceManager.uploadProfileImageonSubmit(strPolicyGroupSeqId,byteArray,1);     


                        System.out.println(status);

                         mapObject.put("status",""+status);





                       //  outputStream.close();
                        // output.close();  




                    }//end of try 



                    catch (TTKException tte) {              
                        tte.printStackTrace();
                        try{
                             String errorMsg="Error While Searching ProfilePicture Data.....";
                             mapObject.put("status", "F");
                             mapObject.put("error_message",errorMsg);       
                            }catch(Exception ie) {
                            ie.printStackTrace();
                            mapObject.put("status", "F");
                            mapObject.put("error_message", "Error While Searching ProfilePicture Data!.....");  
                             }

                    }catch (Exception e) {
                        e.printStackTrace();
                         mapObject.put("status", "F");
                        mapObject.put("error_message", "Error While Searching ProfilePicture Data?.....");              
                   }

                    arra.put(mapObject);
                    //arra.put(mapObject1);
                    return arra.toString();

                }           

当我通过传递所有必需的详细信息而不是其在数据库中成功存储字节数组但从 Postman 或肥皂 UI 工具调用以下 URL 时,但输入流对象中的字节数组不正确,因为如果我正在转换该字节数组在图像文件中也不是那个图像文件,我无法打开。

.请建议我为实现此目的可以做的任何其他事情。帮助将非常可观。
谢谢。

【问题讨论】:

    标签: java arrays rest web-services file-upload


    【解决方案1】:

    这段代码对我有用(尽管我使用 REST 控制台客户端进行测试,而不是 Postman 或 SoapUI)

        @POST
          @Path("/uploadProfileImage")
          @Produces(MediaType.TEXT_PLAIN)
          @Consumes(MediaType.APPLICATION_OCTET_STREAM)
          public String saveImage(byte[] bytes) throws IOException {
    
          this.saveImageToFile("D:\Download Here\exist.jpg", bytes);
           return "Image saved";
          }
    
    
          private void saveImageToFile(String savedImg, byte[] imgData) throws IOException {
    
          InputStream in = new ByteArrayInputStream(imgData);
           try {
            BufferedImage buffImage = ImageIO.read(in);
            ImageIO.write(buffImage, "jpg", new File(savedImg));
           } finally {
            if (in != null)
             in.close();
           }
    
     }
    

    【讨论】:

      【解决方案2】:

      感谢 dsp_user 我使用了 MediaType.APPLICATION_OCTET_STREAM 和来自邮递员的我传递的二进制图像比它的工作正常

      @SuppressWarnings("unchecked")
                  @POST
                  @Path("/UploadProfileImage")
                  @Consumes(MediaType.APPLICATION_OCTET_STREAM) 
                  @Produces(MediaType.APPLICATION_JSON)
                  public String uploadProfileImage(@FormDataParam("file")InputStream uploadedImageInputStream,@HeaderParam("mPolicyGroupSeqId")String PolicyGroupSeqId){
      
                      JSONArray arra = new JSONArray();           
                      LinkedHashMap<String, String> mapObject = new LinkedHashMap<String, String>();
      
                      try {
      
      
      
                          System.out.println("upload profile image");
                          String strPolicyGroupSeqId=TTKCommon.checkNull(PolicyGroupSeqId);
      
                          WebServiceManager webServiceManager=this.getWebServiceManagerObject();
      
                          System.out.println("mPolicyGroupSeqId      "+PolicyGroupSeqId);
      
                          byte[] bytes = new byte[1024];  
      
                          bytes   =   org.apache.commons.io.IOUtils.toByteArray(uploadedImageInputStream);
      
                          /*
                          String filePath = "D:/Download Here/hello.png";
      
                          FileOutputStream fos = new FileOutputStream(filePath);
                          BufferedOutputStream outputStream = new BufferedOutputStream(fos);
                          outputStream.write(bytes);
      
                          fos.flush();
                          fos.close();
                          outputStream.flush();
                          outputStream.close();*/
      
                          System.out.println("kbbsssssssssss  "+bytes.length);
      
      
      
      
      
                          int status=webServiceManager.uploadProfileImageonSubmit(strPolicyGroupSeqId,bytes,1);     
      
      
      
                          if(status==1){
                              mapObject.put("status", "P");
                              mapObject.put("message","Profile Picture Uploaded Successfully");
                          }else{
                              mapObject.put("status", "F");
                              mapObject.put("error_message","Profile Picture Not Uploaded Successfully");
      
                          }
      
                          System.out.println(status);
      
                      }//end of try 
      
      
      
                      catch (TTKException tte) {              
                          tte.printStackTrace();
                          try{
                              String errorMsg="!Error While Uploading ProfilePicture ...";
                              mapObject.put("status", "F");
                              mapObject.put("error_message",errorMsg);        
                          }catch(Exception ie) {
                              ie.printStackTrace();
                              mapObject.put("status", "F");
                              mapObject.put("error_message", "!Error While Uploading ProfilePicture Data ...");   
                          }
      
                      }catch (Exception e) {
                          e.printStackTrace();
                          mapObject.put("status", "F");
                          mapObject.put("error_message", "!Error While Uploading ProfilePicture Data...");                
                      }
      
                      arra.put(mapObject);
                      return arra.toString();
      
      
                  }   //end of uploadProfileImage     
      

      【讨论】:

        猜你喜欢
        • 2023-03-20
        • 2021-05-05
        • 1970-01-01
        • 1970-01-01
        • 2012-02-02
        • 1970-01-01
        • 2016-07-03
        • 2023-03-10
        • 2019-12-15
        相关资源
        最近更新 更多