从Android上传Image Byte Array File到服务器jersey?

时间:2016-11-29 09:16:14

标签: java android arrays file-upload jersey

我正在进行图像比较项目,我从URL下载图像并将其与先前存储在我的Android移动设备上的图像进行比较。 首先,我将用户的图像注册为字节数组,然后将图像发送到服务器以便以后下载。

下载部分没有问题。我手动将字节数组图像文件从手机文件夹复制到服务器文件夹,然后下载相同的文件,它与现有图像匹配。但问题是上传部分,文件上传到服务器文件夹,但它与现有文件不匹配。 我的文件内容没有正确上传,但我不确定。

请分享从android到jersey的上传字节数组图像的任何来源。

我在这里分享了我的上传代码供您考虑。

try {
            // open a URL connection to the Servlet
            FileInputStream fileInputStream = new FileInputStream(sourceFile);
            URL url = new URL(upLoadThumprintUri);

            // Open a HTTP  connection to  the URL
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoInput(true); // Allow Inputs
            conn.setDoOutput(true); // Allow Outputs
            conn.setUseCaches(false); // Don't use a Cached Copy
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Connection", "Keep-Alive");
           // conn.setRequestProperty("ENCTYPE", "multipart/form-data");
            conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
            conn.setRequestProperty("uploaded_file", fileName);

            dos = new DataOutputStream(conn.getOutputStream());

            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""
                    + name + "\"" + lineEnd);

            dos.writeBytes(lineEnd);

            // create a buffer of  maximum size
            bytesAvailable = fileInputStream.available();

            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];

            // read file and write it into form...
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);

            while (bytesRead > 0) {

                dos.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);

            }

            // send multipart form data necesssary after file data...
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

            // Responses from the server (code and message)
            serverResponseCode = conn.getResponseCode();
            //String serverResponseMessage = conn.getResponseMessage();

            //Log.i("uploadFile", "HTTP Response is : "                        + serverResponseMessage + ": " + serverResponseCode);


            if (serverResponseCode == 200) {

                runOnUiThread(new Runnable() {

                    public void run()
                    {
                        runOnUiThread(new Runnable() {
                            public void run() {
                                String msg = "File Upload Completed.\n\n See uploaded file here : \n\n" + " serverpath"
                                        + uploadFileName;
                                mTxtMessage.setText(msg);

                            }
                        });                          
                    }
                });
            }

            //close the streams //
            fileInputStream.close();
            dos.flush();
            dos.close();

        } catch (MalformedURLException ex) {

            //dialog.dismiss();
            ex.printStackTrace();

            runOnUiThread(new Runnable() {
                public void run() {
                    mTxtMessage.setText("MalformedURLException Exception : check script url.");                        
                }
            });                
        } catch (Exception e) {                

            runOnUiThread(new Runnable() {
                public void run() {
                    Log.e("Upload file to server", "Got exception=====: ");                        
                }
            });                
        }

我的服务器代码:

@POST
@Path("/TEMPLATE")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadTemplateFile(@FormDataParam("uploaded_file") InputStream is,
        @FormDataParam("uploaded_file") FormDataContentDisposition formData) throws IOException
{       
    String fileLocation=formData.getFileName();
    String location="C:/Applications/Images";

    System.out.println("file name=="+fileLocation);
    BufferedReader br=new BufferedReader(new InputStreamReader(is));

    String line="";
    ArrayList list = new ArrayList();       

    try {
        while((line=br.readLine())!=null)
        {
            System.out.println(line);
            list.add(line);
        }
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    System.out.println(list);   

    String a[]=new String[list.size()];
    System.out.println(list.size() +"::"+ a.length);    

    File dir = new File(location);              
    if (!dir.exists()) {
        dir.mkdirs();
    }

    File txtFile = new File(dir, fileLocation);     

    FileWriter fw=null;
    try {
        fw = new FileWriter(txtFile,true);
        for(int i=0;i<a.length;i++)
        {
            a[i] = list.get(i).toString();
            fw.append(a[i]);
            fw.append("\r\n");
        }
        //fw.write(line);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally
    {
        try {
            fw.flush();
            fw.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }       
    return Response.status(Status.OK).entity(line).build();             
}

先谢谢。

0 个答案:

没有答案