0
我想在新瀏覽器中打開文件,而不是將其保存爲本地文件。如何編寫CSV文件並在瀏覽器中打開
下面是我的servlet,它保存到我的本地驅動器,但我想在瀏覽器中打開它。我認爲,而不是FileWriter我應該使用OutPutStream,但一些我如何理解如何使用它。
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Connection conn = null;
ResultSet result = null;
PreparedStatement selectStmt = null;
try {
conn = DBUtil.getConnection();
if(request.getParameter("param1").equals("RED9")){
selectStmt = conn.prepareStatement("select * from TableName");
}else if(request.getParameter("param1").equals("RED921")){
selectStmt = conn.prepareStatement("select * from TableName");
result = selectStmt.executeQuery();
FileWriter f = new FileWriter("c://myFile.cvs);
while(result.next()){
writer.append(result.getString("PARTC_SSN"));
writer.append(',');
writer.append("\n");
}
writer.flush();
writer.close();
}catch(Exception e){
e.printStackTrace();
}finally {
try {
if (result != null) {
result.close();
result = null;
}
if (selectStmt != null) {
selectStmt.close();
selectStmt = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
DBUtil
.log("Exception from ExcelReportServlet class, Method -createNoEcmatsRegDoc "
+ e);
}
}
}
謝謝你的工作。 – tom