私はこの質問をしており、他の人が学べるように質問に答えるつもりです。その非常にシンプルで簡単です。役に立てば幸いです。
これです
blob フィールドを使用して、顧客などのエンティティを作成します
@Entity
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Lob
private byte[] logo;
//setter and getter required
たとえば、エンティティとの通信に役立つセッション Bean を作成します。
@Stateless
public class CustomerService {
@PersistenceContext(unitName = "ImageTestPU")
private EntityManager em;
public void persist(Object object) {
em.persist(object);
}
public List<Customer> customerList(){
return em.createNamedQuery(stat).getResultList();
}
public byte[] loadImage(int id){
return em.find(Customer.class, id).getLogo();
}
}
マネージド Bean を作成します。に注意してください private UploadedFile uploadedFile;
。
org.apache.myfaces.custom.fileupload.UploadedFile; 後で説明します。
public class CustomerManager {
@EJB
private CustomerService customerService;
private Customer customer = new Customer();
private List<Customer> list;
private DataModel<Customer> dataModel;
private UploadedFile uploadedFile;
public CustomerManager() {
}
public void createCustomer() throws IOException{
customer.setId(0);
byte[] file = uploadedFile.getBytes();
customer.setLogo(file);
customerService.persist(customer);
}
public void loadTable(){
dataModel = new ListDataModel<Customer>();
dataModel.setWrappedData(customerService.findStatus(customer));
}
public String view(){
customer = dataModel.getRowData();
return "view.xhtml";
}
//setter and getter for all
//use the loadTable method to load your model table
サーブレット クラスを作成します。あなたのイメージを表示することが最も重要です。次の点に注意してください。これらは最も重要です。
@WebServlet(name = "ImageServlet", urlPatterns = {"/ImageServlet"})
これは web.xml の編集を避けるためです。ファイルのURLを取得するだけです。
int id =Integer.parseInt(request.getParameter("id"));
byte[] image = customerService.loadImage(id);
response.setContentType("image/jpeg");
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(image);
outputStream.close();
上記の行は自明のようです。id
後で xhtml 内の画像を参照するために使用されるので、あまり心配しないでください。
したがって、完全なサーブレットは次のようになります
@WebServlet(name = "ImageServlet", urlPatterns = {"/ImageServlet"})
public class ImageServlet extends HttpServlet {
@EJB
private CustomerService customerService;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int id =Integer.parseInt(request.getParameter("id"));
byte[] image = customerService.loadImage(id);
response.setContentType("image/jpeg");
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(image);
outputStream.close();
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
これは、画像バイトを db にアップロードして保存する create xhtml です。
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:t="http://myfaces.apache.org/tomahawk">
<h:head>
<title> Title</title>
</h:head>
<h:body>
<h:form id="uploadForm" enctype="multipart/form-data">
<h:panelGrid columns="2">
username :<h:inputText value="#{customerManager.customer.username}"/>
Status :<h:inputText value="#{customerManager.customer.status}"/>
<h:outputLabel for="file" value="Select file" />
<t:inputFileUpload id="file" value="#{customerManager.uploadedFile}" required="true" />
<h:message for="file" style="color: red;" />
<h:commandButton value="Create" action="#{customerManager.createCustomer()}"/>
</h:panelGrid>
</h:form>
</h:body>
</html>
これは、データベース内のデータを一覧表示するリスト ページです。ビューページに移動するコマンドリンクが添付されています
<h:dataTable value="#{customerManager.dataModel}" var="list">
<h:column>
#{list.id}
</h:column>
<h:column>
<h:commandLink value="#{list.username}" action="#{customerManager.view}" />
</h:column>
</h:dataTable>
結論として、これがいつか誰かに役立つことを願っています。そして、私はまだ答えられていない質問がたくさんあるので、誰かが最も簡単な方法で問題を解決するのに時間がかかることを願っていますが、私が正しく理解できたら読み続けます私のような人々のために最も簡単な方法で共有します. 以前の言及についてはupload
、トマホーク ライブラリにダウンロードする必要があります。私は人々がそれについてbaluscのブログが好きだと思います. 彼はそれを必要以上に複雑に見せただけです。ブログから必要なものを選んで使用するだけです。彼はそれについて異議の余地のない良い人です。乾杯