相关文章推荐
睡不着的橡皮擦  ·  IllegalArgumentExcepti ...·  1 月前    · 
含蓄的铁板烧  ·  BeanDefinitionOverride ...·  8 月前    · 
儒雅的企鹅  ·  ReplicaSet | Kubernetes·  8 月前    · 
叛逆的长颈鹿  ·  www.diagrams.net/_mob6 ...·  1 年前    · 
发怒的芒果  ·  ChatDOC 与文档对话 - 掘金·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams <h:panelGrid columns="2"> <h:outputText value="File:"/> <h:inputFile id="file" value="#{uploadPage.uploadedFile}"/> </h:panelGrid> <br/><br/> <h:commandButton value="Upload File" action="#{uploadPage.uploadFile}"/> </h:form>

My bean

@Named
@ViewScoped
public class UploadPage {       
    private Part uploadedFile; 
    public void uploadFile(){
                see the following  link about how to save blob image in database  jsfspotlight.blogspot.com/2013/09/…
– hanan Ahmed
                Oct 2, 2013 at 15:17

The SQL database BLOB type is in Java represented as byte[]. This is in JPA further to be annotated as @Lob. So, your model basically need to look like this:

@Entity
public class SomeEntity {
    private byte[] image;
    // ...

As to dealing with Part, you thus basically need to read its InputStream into a byte[]. You can use InputStream#readAllBytes() for this:

InputStream input = uploadedFile.getInputStream();
byte[] image = input.readAllBytes();
someEntity.setImage(image);
// ...
entityManager.persist(someEntity);

Or if you're not on Java 9 yet, then head to Convert InputStream to byte array in Java for alternative ways to read an InputStream into a byte[].

I downloaded the library from apache.igor.onlinedirect.bg//commons/io/binaries/…, and loaded the jar file in my project. I imported it like this import org.apache.commons.io.*;, but it tells me that it could not find it..... – Pavel Oct 2, 2013 at 14:48 Just drop JAR file in /WEB-INF/lib folder. Do not fiddle around in project's properties, for sure not Build Path and likes. If you did it before, make sure that you undo it all. By the way, I edited my answer to show the standard Java API approach. – BalusC Oct 2, 2013 at 14:52

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.