通过 url 动态存储和访问文件

Dynamically store and access file via url

我正在尝试生成要发布到 Facebook 的提要文件。他们需要 URL 才能点击并抓取文件。我想知道我应该采取什么方法来存储文件并使文件 public 仅供 Facebook 使用。

我的第一个方法是将 CSV 文件存储在位于 webapp/feed/{filename.csv} 的名为 "feed" 的 public 文件夹中,但是,我已经了解到使用 Faces.getExternalContext().getRealPath("/") 并不是最好的方法,因为文件在重新部署或服务器重启时会丢失。这种方法似乎只适用于我的本地环境,不适用于 AWS。

我的下一个方法是将每个用户单独的提要文件存储在我的数据库中,并根据要求检索它们,但我无法找到有关 if/how 的信息,这是可能的。我对如何获取文件并不感到困惑,但对如何设计一个页面使其可供 Facebook 检索感到困惑。

简而言之,我想获取我创建的 CSV 文件,并通过 Facebook 可以识别和抓取的 URL 使其 public 可用。目前,我正在寻找 Amazon Elastic File System 来解决这个问题。这是正确的方法还是有更简单的解决方案。

感谢您的阅读。

此答案从特定用户数据库中获取提要文件。 Feed 文件只是一个 CSV 文件,其中包含具有 Facebook 所需值的产品信息。当通过 Facebook 安排数据馈送时,他们会点击给定的 url(例如,https://www.yourwebsite.com/feed.xhtml?user={some_user})。 url 应该向请求者发送一个文件。这个 xhtml 文件和 java class 正是这样做的。请记住,此示例不显示文件是如何生成的,只显示文件是如何返回的。

feed.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui"
    xmlns:adm="http://github.com/adminfaces">
    
    <h:body>
        <!-- Meta Data -->
        <f:metadata>

            <f:viewParam name="user" value="#{feed.userId}"
                converter="javax.faces.Long" />
            
            <f:event type="preRenderView" listener="#{feed.init}" />

        </f:metadata>
    
    </h:body>
    
</html>

Feed.java

package me.walkerworks.bean;

import java.io.IOException;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

import org.omnifaces.util.Faces;
import me.walkerworks.security.DataConnect;


/**
 * 
 * @author Thomas Walker 6/18/2020
 *
 */
@Named
@RequestScoped
public class Feed implements Serializable {

    private static final long serialVersionUID = 1L;
    private String userId;
    
    private int feedId;
    private byte[] content;

    /**
     * Initialized Bean
     * 
     * @throws ClassNotFoundException
     * @throws SQLException
     * @throws IOException 
     */
    public void init() throws ClassNotFoundException, SQLException, IOException {

        if (userId != null) {

            // Connection var
            Connection connection = null;

            // Loading jdbc driver for mysql
            Class.forName("com.mysql.cj.jdbc.Driver");

            // Getting connection from products table
            connection = DataConnect.getConnection("user" + userId);

            // Prepare Statement
            PreparedStatement preparedStatement = connection

                    .prepareStatement("SELECT * FROM feeds");

            // Execute statement and get result
            ResultSet resultSet = preparedStatement.executeQuery();

            // If there is data to load from db
            if (resultSet.next()) {

                // Set feed id
                feedId = resultSet.getInt("id");
                

                // Get and store the file in class
                if (resultSet.getBytes("content") != null) {

                    // Get long blob 
                    content = resultSet.getBytes("content");

                }
            }

            // Closing connections
            resultSet.close();
            preparedStatement.close();
            DataConnect.close(connection);
            
            // If content isn't null
            if (content != null) {
                
                // Send file
                Faces.sendFile(content, feedId + ".csv", true);
                
            }           
        }
    }

    /**
     * @return the userId
     */
    public String getUserId() {
        return userId;
    }

    /**
     * @param userId the userId to set
     */
    public void setUserId(String userId) {
        this.userId = userId;
    }

    /**
     * @return the feedId
     */
    public int getFeedId() {
        return feedId;
    }

    /**
     * @param feedId the feedId to set
     */
    public void setFeedId(int feedId) {
        this.feedId = feedId;
    }

    /**
     * @return the content
     */
    public byte[] getContent() {
        return content;
    }

    /**
     * @param content the content to set
     */
    public void setContent(byte[] content) {
        this.content = content;
    }

}