在RESTful中读取用户上载CSV文件

时间:2015-10-06 21:05:37

标签: java rest csv

我尝试在RESTful中保存用户上传csv文件,InputStream inputStream确实在资源级别携带一些数据,但是当它读取inputStream时,没有数据无法读取!

@POST
@Path("/leveloverride")
@Consumes(MediaType.MULTIPART_FORM_DATA)    
public Response handleSellerOverride(@FormDataParam("file") InputStream is, @FormDataParam("file") FormDataContentDisposition fileInputDetails) throws IOException {
CSVReader csvReader = getCSVReader(is, fileInputDetails);
…

}

private CSVReader getCSVReader(InputStream inputStream, FormDataContentDisposition fdcDisposition) throws IOException{
    OutputStream outputStream = null;
    String qualifiedUploadFilePath = UPLOAD_FILE_SERVER + fdcDisposition.getFileName();

    try {
        outputStream = new FileOutputStream(new File(qualifiedUploadFilePath));
        int read = 0;
        byte[] bytes = new byte[1024];
        while ((read = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }
        outputStream.flush();
    }
    catch (IOException ioe) {
        ioe.printStackTrace();
    }
    finally{
        outputStream.close();
    }
    CSVReader csvReader = new CSVReader(new FileReader(qualifiedUploadFilePath));
    return csvReader;

}

0 个答案:

没有答案