gpt4 book ai didi

java - 检测并报告已完成的 Java 下载

转载 作者:行者123 更新时间:2023-12-03 22:55:34 24 4
gpt4 key购买 nike

我正在开发一个网络应用程序 (Java/JSP),其中一部分是允许用户仅“一次”下载请求的文件。问题是当他们点击“下载”按钮时,他们将被要求保存/打开或取消文件,无论他们回应什么,文件都将被标记为已下载,用户将无法再次下载。我正在尝试找出一种方法,当用户以“取消”响应时不将文件计为已下载,并检查用户是否真的完全下载了文件。

这是 Java 部分:

@WebServlet("/download")
public class download extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final int DEFAULT_BUFFER_SIZE = 10240;
/**
* @see HttpServlet#HttpServlet()
*/
public download() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {

Class.forName("org.sqlite.JDBC");
Connection c = DriverManager.getConnection("jdbc:sqlite:C:\\sqlite\\mascapp.db");
c.setAutoCommit(false);
Cookie[] cookies = request.getCookies();
if(request.getSession().getAttribute("aeid") == null || request.getSession().getAttribute("uid") == null)
{
response.sendRedirect("/index.jsp");
}
int ae_num = Integer.parseInt(request.getSession().getAttribute("aeid").toString());




String sql = "SELECT file, filename FROM reports INNER JOIN download USING(tipid) WHERE reports.tipid = ?"+
"AND download.ts_" + ae_num+ " = 0;";

PreparedStatement stmt = c.prepareStatement(sql);
String tipNum = request.getParameter("tipid");
if (tipNum != null) {

stmt.setString(1, tipNum);
//stmt.setString(2, tipNum);
ResultSet res = stmt.executeQuery();
BufferedInputStream fileBlob = null;
String filename = "";

while (res.next()) {
fileBlob = new BufferedInputStream(res.getBinaryStream("file"), DEFAULT_BUFFER_SIZE);

filename = res.getString("filename");
}
if (fileBlob != null) {
System.out.println(filename);
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");

BufferedOutputStream output = new BufferedOutputStream(response.getOutputStream(),
DEFAULT_BUFFER_SIZE);

byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int length;
while ((length = fileBlob.read(buffer)) > 0) {
output.write(buffer, 0, length);
}

output.close();
fileBlob.close();
Date now = new Date();

sql = "UPDATE download SET ts_" + ae_num + " = " + now.getTime() + " WHERE tipid = ?;";
System.out.println(sql);
stmt = c.prepareStatement(sql);
stmt.setString(1, tipNum);

stmt.executeUpdate();
stmt.close();

c.commit();
c.close();
}
else
{

c.close();
response.sendRedirect("/MASC/formdownloaded.jsp");

}
}
else
{
response.getWriter().append("<html><body><h1>Error: no param</h1></body></html>");
c.close();

}
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();

}
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}

任何解决方案或建议?!提前谢谢你。

最佳答案

查看此页面:http://johnculviner.com/jquery-file-download-plugin-for-ajax-like-feature-rich-file-downloads/ (这个链接有时不起作用,如果你有一些问题,这里是 github 的链接:https://github.com/johnculviner/jquery.fileDownload)。

看起来你在这里能做的最好的事情就是通过检查从服务器发回的带有下载文件流的客户端特殊 cookie 来确保下载已开始。为此,您必须像在该线程中一样将 Cookie 添加到 servlet 响应中: Set cookie only after file download complete.

所以在你的 servlet 中的 doGet 方法结束时你应该有这样的东西:

Cookie fileDwnld = new Cookie("fileDownload", "true");
fileDwnld.setPath("/");
response.addCookie(fileDwnld);

这是客户端代码:

<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="jquery.fileDownload.js"></script>
<script>
$(document).ready(function() {
$("#btnSubmit").click(function(){
$.fileDownload('path/to/servlet')
.done(function () { alert('File download a success!'); })
.fail(function () { alert('File download failed!'); });
});
});

关于java - 检测并报告已完成的 Java 下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32572404/

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