简体   繁体   English

使用App Engine Java Servlet动态创建和存储CSV文件

[英]Using app engine java servlet to dynamically create and store csv file

I have a java web application which graphs data using amCharts. 我有一个Java Web应用程序,它使用amCharts绘制数据图形。 The data I want to graph is stored in a SQL database. 我要绘制的数据存储在SQL数据库中。 I have a servlet that queries the database for the data I would like to graph and formats it into a comma delimited csv format. 我有一个servlet,它查询数据库以获取要绘制的数据并将其格式化为逗号分隔的csv格式。 The part I am having an issues with is storing this formatted data into a file after it is created, and then being able to access this file later (through the real path) so I can update the graph. 我遇到的问题是,在创建格式后的数据后将其存储到文件中,然后可以稍后(通过真实路径)访问此文件,以便更新图。

This is what I have so far for the servlet: 到目前为止,这是我对servlet的了解:

public class CreateCsvServlet extends HttpServlet{

public void doGet(HttpServletRequest req, HttpServletResponse response)  throws IOException, ServletException{
    String macAdd = (String) req.getSession().getAttribute("macAddress");

    String path =  getServletContext().getRealPath("/powerData.txt");   //the file "powerData.txt" is an empty file that was deployed with the web app

    final String DB_URL="jdbc:google:mysql://mydatabase:iot?user=root";
    Statement stmt = null;
    Connection conn = null;            

      try{
         // Register JDBC driver
         Class.forName("com.mysql.jdbc.GoogleDriver");

         // Open a connection
         conn = DriverManager.getConnection(DB_URL);

         // Execute SQL query
         stmt = conn.createStatement();

         String sql = "SELECT `wattSeconds`,`time` FROM `iot_data`.`Usage Data` WHERE `MACAddress`='" + macAdd +"'";

         rs = stmt.executeQuery(sql);

         PrintWriter csvWriter = new PrintWriter(new File(path));

         // Extract data from result set
         while(rs.next()){
            //Retrieve by column name
            String watt  = rs.getString("wattSeconds");
            String time = rs.getString("time");
            String row = watt + "," + time;

            csvWriter.println(row) ;
         } 

         csvWriter.close(); 

         // Clean-up environment
         rs.close();
         stmt.close();
         conn.close();
      }catch(SQLException se){
         //Handle errors for JDBC
         se.printStackTrace();
      }

I am trying to use a PrintWriter to write to the file, but I am not sure if this is the right approach. 我正在尝试使用PrintWriter写入文件,但是我不确定这是否是正确的方法。 Also, I am giving the path for a file that already exists to the new File(path) and I feel like this is incorrect. 另外,我将一个已经存在的文件的路径提供给new File(path) ,我觉得这是不正确的。 Want I want to actually happen is to check if the file exists, and if it does it should overwrite the file with this new content. 我要实际发生的事情是检查文件是否存在,如果存在,则应使用此新内容覆盖文件。

You cannot write to the files on App Engine. 您无法在App Engine上写入文件。 You have two options: 您有两种选择:

  1. Store this file as a long String object in the datastore (it must be under 1MB). 将此文件作为长String对象存储在数据存储区中(该文件必须小于1MB)。

  2. Write it to a file in Google Cloud Storage. 将其写入Google Cloud Storage中的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM