我有一个带有下载servlet的gwt Web应用程序,我计算了db中每个文件的下载量。当用户向服务器发送下载请求时,我的下载servlet运行两次,我的下载计数器每次计数2次。但用户已发送请求的文件已下载一次。我不知道我的servlet有2次运行1次请求。
我的下载servlet:
public class DownloadServlet extends HttpServlet {
private static final String UPLOAD_DIRECTORY = "c:\\update\\";
private Connection con;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Statement select=null;
ResultSet result=null;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/laplasdb?useUnicode=true&characterEncoding=UTF-8","gwt","root");
}catch(Exception ex){
}
String query = "SELECT * FROM TBL_Drive_Files WHERE id = '"+req.getParameter("fileid")+"'", filename="";
InputStream filecontent=null;
int filesize=0;
try {
select = con.createStatement();
result = select.executeQuery(query);
while (result.next()) {
filename = result.getString(2);
filesize = result.getInt(4);
filecontent = result.getBinaryStream(5);
}
result.close();
} catch(SQLException e) {
}
BufferedOutputStream output = null;
try {
resp.reset();
resp.setContentType("application/octet-stream");
resp.setContentLength(filesize);
resp.setHeader("Content-disposition", "attachment; filename=\"" + filename + "\"");
output = new BufferedOutputStream(resp.getOutputStream());
for(int data; (data=filecontent.read()) != -1;) {
output.write(data);
}
output.flush();
query = "Update TBL_Drive_Files SET count = count + 1 WHERE id = '"+req.getParameter("fileid")+"'";
try {
select = con.createStatement();
select.execute(query);
select.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
catch (IOException e) {
e.printStackTrace();
}
finally {
}
}
}
在客户端:
Button bd = new Button("Download", new ClickHandler(){
public void onClick(ClickEvent event){
final String link = GWT.getModuleBaseURL() + "download?fileid=3";
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,link);
try {
builder.sendRequest(null, new RequestCallback() {
public void onError(Request request, Throwable t) {
Window.alert("Error bei getExcel");
}
public void onResponseReceived(Request request,Response response){
int statuscode = response.getStatusCode();
if(statuscode == 200) {
Window.Location.replace(link);
} else if(statuscode == 404) {
Window.alert("Service not available.");
}
}
});
} catch (RequestException re) {
Window.alert(re.toString());
}
}
});
RootPanel.get().add(bd);
答案 0 :(得分:2)
你发了两个请求。一个致电builder.sendRequest(null, new RequestCallback()
以及状态为200 Window.Location.replace(link)
顺便说一下,我希望这不是将要部署的代码。
还有很多其他要点,但你应该先解决这个问题,然后你就能找到错误并自己维护代码
答案 1 :(得分:0)
取自我的回答:Download file using Jersey and RestyGWT
In general(*) you cannot use ajax to download files, so you have to use a Window.open() or an iframe to ask the user to save the file as.
所以不要使用RequestBuilder
来获取文件,正如我所说:Window.open in GWT not open correctly with in a call back function
The best solution, is to open the file in an iframe, but you have to set the appropriate content-disposition header in server side which causes the browser to show the "Save" dialog.