2016-02-17 47 views
0

我有一個JSP頁面,在後端使用JPA-REST,我設法將一個blob插入到數據庫中。現在我想能夠從數據庫中獲取/獲取 blob,但似乎無法找到任何有關如何通過Jersey來執行此操作的示例,而不是使用servlet(我對創建自己的REST非常新穎服務)。使用JPA,Jersey從數據庫中檢索Blob(pdf)

這是我用來插入斑點到數據庫的代碼:

@POST 
@Path("upload/{id}") 
@Consumes({"application/x-www-form-urlencoded", "multipart/form-data"}) 
public void addBlob(@PathParam("id") Integer id, @FormDataParam("file") InputStream uploadedInputStream) throws IOException { 
    ClientCaseDoc entityToMerge = find(id); 
    try { 
     ByteArrayOutputStream out = new ByteArrayOutputStream(); 
     int read = 0; 
     byte[] bytes = new byte[1024]; 
     while ((read = uploadedInputStream.read(bytes)) != -1) { 
      out.write(bytes, 0, read); 
     } 
     entityToMerge.setDocument(out.toByteArray()); 
     super.edit(entityToMerge); 
    } 
    catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

有沒有的任何類似的方式檢索從數據庫中的BLOB?還是我必須使用servlet?

任何幫助,非常感謝。

+0

@BorisPavlović我認爲,這些問題的答案都不太清楚,說實話,他們中的一個創建了自己的類的pdfGenerator,另一種是創建一個QR碼爲PNG,和另外一個發表自己的代碼來導出Excel(XLSX)正如我在我的問題中提到的,我此刻不使用servlet .. – MattiasH

+0

當然,您正在使用servlet。處理Web請求的任何服務器端Java代碼都是一個servlet。 REST使它更容易,但它仍然是一個servlet。 –

+0

@BorisPavlović好的,謝謝,我學到了一些新東西,現在..爲什麼人們在使用REST時創建自己的servlet類?這是沒有道理的.. – MattiasH

回答

1

有沒有類似的方法從數據庫中檢索blob?還是我必須使用servlet?

雖然在問題中沒有提到,但我想說的是通過Jersey返回BLOB而不是使用Servlets。如果我錯了,請在評論中糾正我。如果我是正確的,你可能希望更新你的問題提到澤西島。

這個問題我認爲是Input and Output binary streams using JERSEY?的副本。然而,這些評論似乎顯示出一些混淆如何在OP案例中實施它。當您在域模型中加載PDF時(如果這是一件好事,我會讓其他人爭論),流式傳輸是不需要的。所有你需要做的就是創建一個Response,將entity設置爲數據層返回的字節數組。

@Path("upload/{id}") 
@GET 
public Response getPDF(@PathParam("id") Integer id) throws Exception { 
    ClientCaseDoc entity = find(id); 
    return Response 
      .ok() 
      .type("application/pdf") 
      .entity(entity.getDocument()) // Assumes document is a byte array in the domain object. 
      .build(); 
} 
1

這已經回答了,但幫助更廣泛的問題;

我有一個實體;

@Entity 
public class BlobEntity { 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Long id; 

    @Column(name = "NAME") 
    private String name; 

    @Lob 
    @Column(name="DATA", length=100000) 
    private byte[] data; 

一個JPA庫

@Repository 
public interface BlobEntityRepository extends CrudRepository<BlobEntity, Long> { 
} 

和測試,讀取Word文檔,並從數據庫中檢索它

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = "classpath:applicationContext-test.xml") 
public class BlobEntitytRepositoryTest extends AbstractTest { 

    @Autowired 
    private BlobEntityRepository repository; 

    @Test 
    @Transactional 
    public void test1() throws IOException { 

     InputStream inputStream = getClass().getResourceAsStream("/HelloGreg.docx"); 
     byte[] byteArray = IOUtils.toByteArray(inputStream); 

     BlobEntity blobEntity = new BlobEntity(); 
     blobEntity.setName("test"); 
     blobEntity.setData(byteArray); 

     repository.save(blobEntity); 

     assertEquals(1, repository.count()); 

     BlobEntity entity = repository.findOne(1l); 
     assertNotNull(entity); 

     FileOutputStream outputStream = new FileOutputStream(new File("testOut.docx")); 
     IOUtils.write(entity.getData(), outputStream); 
    } 

} 

配置

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:cache="http://www.springframework.org/schema/cache" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd 
    http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> 

    <context:component-scan base-package="com.greg" /> 
    <tx:annotation-driven /> 
    <jpa:repositories base-package="com.greg" /> 

    <bean id="dataSource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="org.h2.Driver" /> 
     <property name="url" value="jdbc:h2:file:~/data/jpa-test" /> 
     <property name="username" value="sa" /> 
     <property name="password" value="" /> 
    </bean> 

    <bean id="entityManagerFactory" 
     class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="packagesToScan" value="com.greg" /> 
     <property name="jpaVendorAdapter"> 
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /> 
     </property> 
     <property name="jpaProperties"> 
      <props> 
       <prop key="hibernate.hbm2ddl.auto">create</prop> 
       <prop key="hibernate.show_sql">false</prop> 
       <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop> 
      </props> 
     </property> 
    </bean> 

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory" /> 
    </bean> 

</beans> 
+0

是否有可能做到這一點,而不使用Spring和不使用JPA存儲庫?我現在所擁有的是Entity類,FacadeREST,pom.xml,AbstractFacade,Application Config,JSP頁面。 – MattiasH

+0

我想要做的是在@Path(「下載/ {id}」)中,我想查找連接到該{id}的blob並將其發送回JSP頁面作爲響應 – MattiasH

+0

您需要使用JPA (API)和Hibernate(實現,其實很簡單,我已經添加了spring配置 –