通过Rest WebService上载图像文件 [英] Image File Upload Through Rest WebService

查看:69
本文介绍了通过Rest WebService上载图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have written below code to do it- :)

                @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或soap UI工具通过在数据库中成功传递所有比其存储字节数组所需的详细信息来调用URL以下的URL时,输入流对象中传入的字节数组不合适,因为如果我要转换该字节数组图像文件中的图像文件也无法打开.

when i am calling below URL from Postman or soap UI tool by passing all required details than its storing byte array successfully in database but that byte array which is coming in input stream object is not proper because if i am converting that byte array in image file also than that image file , i am not able to open.

.请向我建议我为实现此.help可以做的任何其他事情.
谢谢.

.please suggest me any other thing i can do for achieving this .help will be highly appreciable.
thanks.

推荐答案

感谢dsp_user我使用了MediaType.APPLICATION_OCTET_STREAM,并且从邮递员那里我以二进制格式传递了比它正常工作的图像

Thanks dsp_user I uesd MediaType.APPLICATION_OCTET_STREAM and from postman i am passing image in binary than its working fine

@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     

这篇关于通过Rest WebService上载图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆