小编典典

如何在数据库中上传照片以及如何在jsp页面中检索

jsp

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

<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文件中编写的将数据存储在数据库中的逻辑:

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();
    }
}

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

这是我的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"/>

阅读 308

收藏
2020-06-10

共1个答案

小编典典

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

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

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

编辑:提示获取InputStream

            InputStream str = (InputStream)uploadRequest.getFileAsStream("filePath", false);
2020-06-10