如何在android上传图片?

时间:2014-10-20 11:04:37

标签: java android

服务器代码

 @POST
     @Path("/feedback1")
     @Consumes(MediaType.MULTIPART_FORM_DATA)
     @Produces(MediaType.APPLICATION_JSON)
     public ServiceResponse Feedback1(@FormDataParam("file") InputStream file_in,@FormDataParam("file") FormDataContentDisposition fileDetail)
     {
      final ServiceResponse response = new ServiceResponse();
      Boolean flag=false;int size = 0;
      String fileName = null;
      fileName = fileDetail.getFileName().trim();
      try{
       File file = new File(System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") +fileDetail.getFileName());
       FileOutputStream fos = new FileOutputStream(file);
       int read =0;
       byte[] bytes = new byte[1024];
       while ((read = file_in.read(bytes)) != -1) {
        fos.write(bytes, 0, read);
       }
       size=(int) (file.length()/1024);
       System.out.println(size);

      }catch(IllegalStateException | IOException e)
      {
       LOG.error("problem sending feedback from Feedback method : ", e);
       flag=true;
       ServiceUtil.setFailureMessage(response, e.getMessage(), null);
      }
      if (!flag) {
       ServiceUtil.setSuccessMessage(response, SUCCESS +" filename= "+ fileName + " size =" + size +"KB", null);
      }
      return response;
     }

你好

我正在服务器上传图片。我能够获得图像字节现在我需要在服务器上传这个图像你能不能告诉我如何实现这个目标 我收到此错误

java.net.ProtocolException:不支持输出

这是我的代码。我在该函数中传递了图像的字节。但是我在这一行上收到错误

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

private void uploadFile(byte[] bytes) {
    // TODO Auto-generated method stub
     HttpURLConnection conn = null;
     DataOutputStream dos = null;  
     String lineEnd = "\r\n";
     String twoHyphens = "--";
     String boundary = "*****";
      try {
        URL url = new URL("http://192.XXX.X2.X:8080/FGRailApps/jservices/rest/feedback1");

          // Open a HTTP  connection to  the URL
          conn = (HttpURLConnection) url.openConnection(); 
          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);

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

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

          dos.writeBytes(lineEnd);
          dos.write(bytes);
          dos.writeBytes(lineEnd);
          dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

          // Responses from the server (code and message)
          serverResponseCode = conn.getResponseCode();
          String serverResponseMessage = conn.getResponseMessage();
          Log.d("---------------", serverResponseMessage);

    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 



}

1 个答案:

答案 0 :(得分:0)

public static boolean Upload(String sourceFileName) {
    String uuid = UUID.randomUUID().toString();
    System.out.println("uuid = " + uuid);
    FTPClient ftpClient = new FTPClient();
    // File sdCard = Environment.getExternalStorageDirectory();
    File file = new File(sourceFileName);       
    try {
        ftpClient.connect(InetAddress.getByName(FTPCONSTANTS.HOST_NAME));
        ftpClient.login(FTPCONSTANTS.USER_NAME, FTPCONSTANTS.PASSWORD);
        ftpClient.changeWorkingDirectory(MessageActivity.Path1);    

        //if (ftpClient.getReplyString().contains("250")) {
            Log.e("%%%%%%%%%", "888888888");

            ftpClient
                    .setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
            BufferedInputStream buffIn = null;
            ftpClient.enterLocalPassiveMode();
            FileInputStream in = new FileInputStream(file);




                result = ftpClient.storeFile(MessageActivity.Path1 + uuid
                        + "." + "mp4", in);

                result1 = MessageActivity.Path1 + uuid + "." + "mp4";
                System.out.println("cjkkkkkkkkkkkkkkkk " + result1);






            /*
             * result = ftpClient.storeFile( FTPCONSTANTS.FOLDER_PATH + "/"
             * + sourceFileName.substring( sourceFileName.lastIndexOf("/") +
             * 1, sourceFileName.length()), in);
             */
            in.close();
            if (result)
                Log.v("upload result", "succeeded");
            ftpClient.logout();
            ftpClient.disconnect();
        //}

    } catch (SocketException e) {
        Log.e("", e.getStackTrace().toString());
    } catch (UnknownHostException e) {
        Log.e("", e.getStackTrace().toString());
    } catch (IOException e) {
        Log.e("", e.getStackTrace().toString());
    }
    return result;

}