如何在数据库中上传照片以及如何在jsp页面中检索 [英] how to upload photo in database and how to retrive in jsp page

查看:73
本文介绍了如何在数据库中上传照片以及如何在jsp页面中检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在提供我的service.xml文件,其中包含表的详细信息:

I'm providing my service.xml file which contains the table details:

<entity name="Testimonial" local-service="true" remote-service="false">
    <!-- PK fields -->
    <column name="TestimonialId" type="long" primary="true" />

    <!-- UI fields -->
    <column name="subject" type="String" />
    <column name="area" type="String" />
    <column name="username" type="String" />
    <column name="email" type="String" />
    <column name="photo" type="String"/>
    <column name="company" type="String" />
    <column name="designation" type="String" />

    <!-- Audit fields -->
    <column name="createdAt" type="Date" />

这是我的Java文件,在其中写入了将数据存储在数据库中的逻辑:

Here is my java file where I have written the logic of storing data in database:

public void updateTesti(ActionRequest actionRequest,ActionResponse actionResponse) throws IOException, PortletException
{
    String subject = ParamUtil.getString(actionRequest,"subject");
    String area = ParamUtil.getString(actionRequest,"area");
    String username = ParamUtil.getString(actionRequest,"username");
    String email = ParamUtil.getString(actionRequest,"email");
    String company = ParamUtil.getString(actionRequest,"company");
    String designation = ParamUtil.getString(actionRequest,"designation");

    System.out.println("Your inputs ==> " + subject + ", " + area + "," 
    + username + "," + email + "," + company + "," + designation);

    Testimonial T1 = new TestimonialImpl();

    // set primary key
    long TestimonialId = 0L;
    try {
        TestimonialId =
        CounterLocalServiceUtil.increment(
        this.getClass().getName());
    } catch (SystemException e) {
        e.printStackTrace();
    }
    T1.setTestimonialId(TestimonialId);

    UploadPortletRequest uploadRequest = 

    PortalUtil.getUploadPortletRequest(actionRequest);
    String filePath = uploadRequest.getFileName("filePath");

    try{
        java.io.File file = uploadRequest.getFile("filePath");
        //Manage the Upload

    }catch (Exception e) {
        ///
    }
    // set UI fields
    T1.setSubject(subject);
    T1.setArea(area);
    T1.setUsername(username);
    T1.setEmail(email);
    T1.setCompany(company);
    T1.setDesignation(designation);
    T1.setPhoto(filePath);

    // set audit field(s)
    T1.setCreatedAt(new Date());

    // insert the book using persistence api
    try {
        TestimonialLocalServiceUtil.addTestimonial(T1);
    } catch (SystemException e) {
        e.printStackTrace();
    }
}

告诉我我在哪里错了,缺少什么?

Tell me where am I wrong and what is missing?

这是我的JSP代码:

<aui:form name="fm" method="POST" action="<%= updateTestiURL.toString() %>">
    <aui:input name="subject" label="Subject"/>
    <aui:input type="textarea" name="area" label="your Testimonial" />

    <aui:input name="username" label="Username"/>
    <aui:input name="email" label="Email"/>
    <aui:input type="file" label="upload your file"  name="filePath" />
    <aui:input name="company" label="Company"/>
    <aui:input name="designation" label="Designation"/>

    <aui:button type="submit" value="Save"/>

推荐答案

我认为存储图像的路径不是一个好主意.即使您将它们从一台唯一的计算机上载,您也可以访问该路径并使用完整路径,这可能不是您的情况.

I don't think it's a good idea to store the image's Path. Even if you had them uploaded from one and only computer, and you could access that path and work with full paths, which is probably not your case.

由于可以获得java.io.File,因此可以检索InputStream并使用bytes []并将它们存储为Text/String或等效于Blob的东西.

Since you can get a java.io.File, you can retrieve an InputStream, and work with bytes[] and store them either as Text/String or something equivalent to a Blob.

您可以检查Liferay的ImageLocalServiceImpl类的源代码,并查看其如何使用这些资源

You can check in Liferay's source for ImageLocalServiceImpl class and see how it's working with these resources

获取InputStream的提示

Edit : a hint to get InputStream

            InputStream str = (InputStream)uploadRequest.getFileAsStream("filePath", false);

这篇关于如何在数据库中上传照片以及如何在jsp页面中检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆