2011-12-30 30 views
0

我對Java很新穎。我通過JaxRS將文件從網頁表單傳遞給Java。然後我用FileInputStream讀取文件(我也嘗試了一個FileReader,我不知道應該使用哪一個),然後將這些數據轉換爲一個StringBuffer,並將其轉換爲一個StringBuffer.toString() 。從FileInputStream或FileReader中剝離HTTP標頭信息

我注意到,我在我的字符串中獲取HTTP頭信息。像內容處理,文件名和內容類型的東西。在調用StringBuffer.toString()之前是否有一種方便的方法去除這些信息?

這裏是我用不相關的位代碼樣本剝離出來:

public Response method(@FormDataParam("fileToUpload") File file) 
{ 
    StringBuffer strBuffer=new StringBuffer(""); 
    String fileAsString; 

    try 
    { 
     FileInputStream fileInputStream=new FileInputStream(file); 

     int characterIndex; 
     while ((characterIndex = fileInputStream.read()) != -1) 
     { 
      strBuffer.append((char)characterIndex); 
     } 

     fileInputStream.close(); 

     fileAsString=strBuffer.toString(); //I'd like to have the HTTP header info stripped before getting to this point without having to do some sort of manual string manipulation. 
    } 
    catch (FileNotFoundException e){} 
    catch (IOException e){} 
} 

非常感謝您的幫助!

+1

'catch(FileNotFoundException e){}'...請不要這樣做。 – artbristol 2011-12-30 19:28:32

+0

你有沒有嘗試綁定你的FormDataParam到InputStream而不是文件? – Perception 2011-12-30 19:37:41

+0

@artbristol,我的歉意。我只是簡單地刪除所有不相關的代碼。 – EvaUnit02 2011-12-30 19:50:50

回答

1

我解決了它。顯然澤西希望你也傳遞一個FormDataContentDisposition。你不需要對這個論點做任何事情。簡單地通過它將從信息內容中分離標題。