Retrieving binary data [varbinary(max)] from SQL Server 2008 using JSP

大憨熊 提交于 2019-12-08 05:16:13

问题


Here is the query I am using to create and insert binary data in SQL Server 2008
Create query:

CREATE TABLE Employees (
Id int,
Photo varbinary(max) not null,
Name varchar(50) not null,
Sound varbinary(max) not null
)

Insert query:

INSERT INTO Employees SELECT '10',
(SELECT BulkColumn AS E FROM OPENROWSET ( BULK 'd:\1.jpg', Single_Blob) bc), 'John', (SELECT BulkColumn AS E FROM OPENROWSET ( BULK 'd:\2.wav', Single_Blob) bc)

One of the files is .jpg and the other is .wav
How can i know the extension of these files while retrieving?
Do i have to use a query for finding the extension?
OR
Do i have to see content-type after i get the resultset in jsp?


回答1:


You need to add another column for the content type or file name/extension to the DB table. This way you can just insert it along the binary data and retrieve back later.

In a Servlet, you can get the content type based on the file name/extension as follows:

String contentType = getServletContext().getMimeType(filename);
// ...

If you do this before insert, then you can store the content type along. If you do this after insert, then you should store the filename along.

The default mime types from the servlet context are definied in servletcontainer's web.xml, which is in case of for example Tomcat located in its /conf/web.xml. You can add another mime types to your own /WEB-INF/web.xml as well, e.g.

<mime-mapping>
    <extension>svg</extension>
    <mime-type>image/svg-xml</mime-type>
</mime-mapping>

See also:

  • How to check an uploaded file whether it is an image or not?


来源:https://stackoverflow.com/questions/5606161/retrieving-binary-data-varbinarymax-from-sql-server-2008-using-jsp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!