我正在向$.post
servlet 发送请求,发送一些需要保存在文件中的数据。servlet 保存数据,然后发回文件的路径。我正在尝试导航到response
方法回调中所持有的路径,但这是发生了什么:
function saveData(){
var path;
$.post("SaveFileServlet", {data : JSON.stringify(jsonPieces)}, function(response) {
path = response; // setting the path like this does NOT WORK - nothing happens
//path="storing/csv_uploads/test.csv"; // setting the path like this WORKS
alert (path); // shows: storing/csv_uploads/filename.csv - e.g. WHAT IT SHOULD
$("body").append("<iframe src='" + path + "' style='display: none;' ></iframe>");
});
}
如何使它工作,为什么它不工作?谢谢。
更新的编辑
明白了!!!!!!...这是servlet:它可能没有创建文件:真正的路径不是filename.csv
a System.currentTimeMillis()+".csv"
,然后我将它写入服务器。但我只是在创建文件后才发送响应(或者我是吗?)......
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
String filename = "storing/csv_uploads/"+System.currentTimeMillis()+".csv";
//String filename = "storing/csv_uploads/filename.csv"; //this works since it was previously there
PrintWriter out = response.getWriter();
try {
Type type = new TypeToken<List<PieceFeeder>>(){}.getType();
ArrayList<PieceFeeder> pieceFeeders = new Gson().fromJson(request.getParameter("data"), type);
DataManager dm = new DataManager();
if (dm.exportPieces(pieceFeeders, filename)) {
System.out.println("in SaveFileServlet: after exportPieces");
File f = new File(filename);
while (!f.exists()) {
//waiting...I NEVER GET IN HERE
System.out.println("in SaveFileServlet: waiting for file to exist");
}
out.write(filename);
} else {
out.write("error at creating file");
}
}...
}
和DataManager.exportPieces
:
public void exportPieces(ArrayList<PieceFeeder> pieceFeeders, String filename) {
BufferedWriter bufferedWriter = null;
try {
bufferedWriter = new BufferedWriter(new FileWriter(filename));
//write first line without "\n"
if (pieceFeeders.size() > 0) {
PieceFeeder pf = pieceFeeders.get(0);
Piece p = pf.getPiece();
bufferedWriter.write(p.getDescription() + ","
+ pf.getStock() + ","
+ p.getLength() + ","
+ p.getWidth() + ","
+ p.getLengthEdges() + ","
+ p.getWidthEdges());
}
for (int i = 1; i < pieceFeeders.size(); i++) {
PieceFeeder pf = pieceFeeders.get(i);
Piece p = pf.getPiece();
bufferedWriter.write("\n" + p.getDescription() + ","
+ pf.getStock() + ","
+ p.getLength() + ","
+ p.getWidth() + ","
+ p.getLengthEdges() + ","
+ p.getWidthEdges());
}
return true;
} catch (Exception ex) {
Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
return false;
} finally {
//Close the BufferedWriter
try {
if (bufferedWriter != null) {
bufferedWriter.flush();
bufferedWriter.close();
System.out.println("in exportPieces: finally");
}
} catch (IOException ex) {
Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
我总是得到
"in exportPieces: finally"
"in SaveFileServlet: after exportPieces"
并且"in SaveFileServlet: waiting for file to exist"
永远不会被调用,因此文件在发送响应之前就存在
但是如果我发送一个旧文件*一个尚未创建的文件),它就可以工作
较新的编辑
只是为了说清楚:如果我这样做:
window.location = path
$("body").append("<iframe src='" + path + "' ></iframe>");
删除display:none
它给出一个404 error
:The requested resource () is not available.
但标题显示我正在尝试访问:http://localhost:8080/optimizerwithservlet/storing/csv_uploads/filename.csv
所以它就在那里
编辑
正如用户所言,我安装了 Firebug,这就是我得到的:
<iframe style="display: none;" src="storing/csv_uploads/filename.csv">
意思是它有效!但是为什么不显示...?
旧编辑:
万一这可能很重要,尽管我对此表示怀疑:这是来自servlet的相关代码:
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
String filename = "storing/csv_uploads/filename.csv";
PrintWriter out = response.getWriter();
out.write(filename);