使用blobstore上传和解析Google App Engine中的csv

时间:2014-11-07 12:09:16

标签: java google-app-engine gwt blobstore

我试图编写一个在AppEngine上运行的简单Google gwt Java应用程序。

我已经创建了一个jsp上传表单来上传* .csv文件:

<%@ page import="com.google.appengine.api.blobstore.BlobstoreServiceFactory" %>
<%@ page import="com.google.appengine.api.blobstore.BlobstoreService" %>

<%
   BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
%>
<html>
    <head>
        <title>Upload Test</title>
    </head>
    <body>
        <form action="<%= blobstoreService.createUploadUrl("/HelloWorld/ImportHandlerServlet")   %>" method="post" enctype="multipart/form-data">
             <input type="file" name="importCSV">
            <input type="submit" value="Submit">
        </form>
    </body>
</html>

在服务器端,我编写了一个新的servlet,它应该使用openCSV将上传的CSV文件转换为关联的数组:

import au.com.bytecode.opencsv.CSVReader;

public class ImportHandlerServlet extends HttpServlet {
    private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();  
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter out = resp.getWriter();

        Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(req);
        List<BlobKey> bkList = blobs.get("importCSV");
        BlobKey blobKey = bkList.get(0);
        BlobstoreInputStream blobStream = new BlobstoreInputStream(blobKey);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(blobStream));

        //Print out first line to check if BufferedReader is empty
        out.println(bufferedReader.readLine());

        //New CSVReader object reading from bufferedReader
        CSVReader csvReader = new CSVReader(bufferedReader);

        //Add csv-Fields to two-dimensional array 
        List<String[]> rows = csvReader.readAll();
        String output[][] =  rows.toArray(new String[rows.size()][]);

        out.println(output.length);
    }
}

如果我上传文件,bufferedReader会打印出它的第一行。因此bufferedReader不是空的,但是Array不包含任何值。

有没有人看到可能的解决方案?

0 个答案:

没有答案