我已經寫了下面的代碼,通過傳遞所需全部信息比它的存儲字節數組做成功IT- :)圖像文件上傳通過REST WebService的
@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();
}
當我打電話以下網址從郵遞員或肥皂UI工具在數據庫中,但是輸入流對象中的字節數組不正確,因爲如果我將圖像文件中的字節數組也轉換爲該圖像文件,我無法打開。
。請告訴我任何其他的事情,我可以做到這一點。幫助將是非常可觀的。
謝謝。