gpt4 book ai didi

java - 无法使用 AJAX 响应作为文件名(而非内容)

转载 作者:行者123 更新时间:2023-12-02 07:29:40 26 4
gpt4 key购买 nike

我正在发送 $.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但是一个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);

最佳答案

$.post 是异步的。您试图在该值存在之前对其进行提醒,仅此而已。

来自jQuery API documentation

The first letter in Ajax stands for "asynchronous," meaning that the operation occurs in parallel and the order of completion is not guaranteed. The async option to $.ajax() defaults to true, indicating that code execution can continue after the request is made. Setting this option to false (and thus making the call no longer asynchronous) is strongly discouraged, as it can cause the browser to become unresponsive.

关于java - 无法使用 AJAX 响应作为文件名(而非内容),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13090995/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com