内容类型为“application / octet-stream”的JSP上载文件

时间:2011-05-04 03:25:30

标签: jsp

我正在开发一个项目,闪存上传一些内容类型为“application / octet-stream”的二进制数据,我需要将其保存在Oracle数据库的BLOB列中。

我可以使用Commons fileupload吗? 有关此的任何示例代码吗?我可以找到内容类型“multipart / form-data”的代码。

1 个答案:

答案 0 :(得分:-1)

private byte[] getByteArrayFromUploadFile(HttpServletRequest request)throws Exception{
    ServletFileUpload upload = new ServletFileUpload();

    // Parse the request
    FileItemIterator iter = null;
    try{
        iter = upload.getItemIterator(request);
    }catch (Exception e) {
        System.out.println("Error occured during getting iterator");
    }
    byte[] readBytes = null;
    while (iter.hasNext()) {
        FileItemStream item = null;
        try{
            item = iter.next();
         }catch (Exception e) {
            System.out.println("Error occured during Iteration");                
        }

        String name = item.getFieldName();

        if (!item.isFormField()) {
            BufferedInputStream stream = new BufferedInputStream(item.openStream());
            readBytes= new byte[stream.available()];
            System.out.println("total Number of Bytes:"+ readBytes.length);             
            stream.read(readBytes);
        }
    }
   return readBytes;
}
相关问题