2016-10-30 8 views
0

以下のコードでファイルを受け入れるジャージーレストサーバーがあります。ファイルアップロードがAndroidでコンテンツ処理を送信できない

しかし、Android上で、ファイル名を送信することができますが、ファイルの内容を送信するために、そのことはできないWebブラウザ

から送信するときにコンテンツ配置からファイル名を受信することができます。

@POST 
@Path("/upload") 
@Consumes(MediaType.MULTIPART_FORM_DATA) 
public Response uploadFile(

    @FormDataParam("file") InputStream uploadedInputStream, 
    @FormDataParam("file") FormDataContentDisposition fileDetail) { 
      newa.NewClass newca = new newa.NewClass(); 
      try { 
      javax.naming.Context env = (javax.naming.Context)new InitialContext().lookup("java:comp/env"); 
    String uploadedFileLocation = (String)env.lookup("storeImage") + fileDetail.getFileName(); 
    // save it 
      newca.insert_exception("uploadFile name: " , uploadedFileLocation); 
    writeToFile(uploadedInputStream, uploadedFileLocation); 
    String output = "File uploaded to : " + uploadedFileLocation; 
    return Response.status(200).entity(output).build(); 
      } catch(NamingException ne) { 
       newca.insert_exception("uploadFile Exception" , ne.toString()); 
       return Response.status(200).entity("Error").build(); 
      } 
} 

private void writeToFile(InputStream uploadedInputStream, 
    String uploadedFileLocation) { 

    try { 
     OutputStream out = new FileOutputStream(new File(uploadedFileLocation)); 
     int read = 0; 
     byte[] bytes = new byte[1024]; 

     out = new FileOutputStream(new File(uploadedFileLocation)); 
     while ((read = uploadedInputStream.read(bytes)) != -1) { 
      out.write(bytes, 0, read); 
     } 
     out.flush(); 
     out.close(); 

    } catch (IOException e) { 

     e.printStackTrace(); 
    } 

} 

ファイル名を指定してファイルをアップロードすることができ、通常のHTMLページ - - ここで

はジャージーサーバーがある

<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>Upload Test</title> 
</head> 
<body> 
    <form action="rest/hello/upload" method="post" enctype="multipart/form-data"> 
Select image to upload: 
<input type="file" name="file" id="file"> 
<input type="submit" value="Upload Image" name="submit"> 

ではありませんアンドロイドコードファイル名を送信できますが、ファイルコンテンツを送信できる -

すべてのAndroidでのヘッダーで行うにはの
private String server; 

    public AsyncHttpPostTask(final String server) { 
     this.server = server; 
    } 

    @Override 
    protected String doInBackground(String... params) { 
     Log.d("ListView1", "doInBackground"); 
     String iFileName = "ovicam_temp_vid.mp4"; 
     String lineEnd = "\r\n"; 
     String twoHyphens = "--"; 
     String boundary = "*****"; 
     String Tag="fSnd"; 
     URL connectURL; 
     try { 



      connectURL = new URL(server); 

      HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection(); 

      // Allow Inputs 
      conn.setDoInput(true); 

      // Allow Outputs 
      conn.setDoOutput(true); 

      // Don't use a cached copy. 
      conn.setUseCaches(false); 

      // Use a post method. 
      conn.setRequestMethod("POST"); 

      conn.setRequestProperty("Connection", "Keep-Alive"); 

      conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); 

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

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



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



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

      Log.e(Tag,"Headers are written"); 
      Log.d("ListView1", "File: " + params[0]); 
      FileInputStream fileInputStream = new FileInputStream(params[0]); 
      // create a buffer of maximum size 
      int bytesAvailable = fileInputStream.available(); 

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

      // read file and write it into form... 
      int 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); 
      } 
      dos.writeBytes(lineEnd); 
      dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 

      // close streams 
      fileInputStream.close(); 

      dos.flush(); 

      Log.d("ListView1", "File Sent, Response: " + String.valueOf(conn.getResponseCode())); 

      InputStream is = conn.getInputStream(); 

      // retrieve the response from server 
      int ch; 

      StringBuffer b =new StringBuffer(); 
      while((ch = is.read()) != -1){ b.append((char)ch); } 
      String s=b.toString(); 
      Log.d("ListView1" , s); 
      dos.close(); 


     } catch (IOException ie) { 
      Log.d("ListView1", "Upload Files Response:::" + ie.toString()); 
     } 

答えて

0

その -

それは単純にする必要があります -

..... 
DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); 
dos.writeBytes(twoHyphens + boundary + lineEnd); 
dos.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\"" + "gold1.jpeg" +"\"" + lineEnd); 
dos.writeBytes(lineEnd); 
Log.e(Tag,"Headers are written"); 
..... 
関連する問題