我对 Java 还很陌生。我通过 JaxRS 将文件从网页表单传递到 Java。然后,我使用 FileInputStream 读取文件(我也尝试过 FileReader,我不确定应该使用哪个),然后将该数据传输到 StringBuffer 中,以便使用 StringBuffer.toString() 转换为字符串.
我注意到我在 String 中获取了 HTTP header 信息。内容处置、文件名和内容类型等内容。在调用 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){}
}
非常感谢您的帮助!
最佳答案
我解决了。显然 Jersey 希望您也传递 FormDataContentDisposition。您无需对参数执行任何操作。只需传入它即可将 header 与消息内容分开。
关于java - 从 FileInputStream 或 FileReader 中剥离 HTTP header 信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8683148/