WHY? About do wnLoad with Servlet.(100分)

  • 主题发起人 主题发起人 coolsboy
  • 开始时间 开始时间
C

coolsboy

Unregistered / Unconfirmed
GUEST, unregistred user!
HTML :fileDownLoad.htm

<html>
<head>
<title>Untitleddo
cument</title>
<meta http-equiv="Content-Type" content="text/html;
charset=Shift_JIS">
</head>
<body bgcolor="#FFFFFF" text="#000000" >
<FORM method="POST" name="f1" action="/yki/servlet/yki.E050_DownLoad">
<center>
<input type="submit" name="e050_logDo_flg" value="login" >
</center>
</FORM>
</body>
</html>
<-------------------------------------------------------->
servlet :E050_DownLoad.java

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class E050_DownLoad extends HttpServlet implements Constants{
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException{
HttpServletResponse m_response = response;
String filename = "import_error_list";

String filetype = ".txt";

String sourceFilePathName = "C:/kura/"+filename + filetype;
File file = new File(sourceFilePathName);
/*if(!file.exists()){
out.println("filedo
nnot exist");
out.flush();
out.close();
return;
}*/
FileInputStream fileIn = new FileInputStream(file);
long fileLen = file.length();
int readBytes = 0;
int totalRead = 0;
byte b[] = new byte[65000];
110 m_response.setContentType("application/x-msdownload");
m_response.setContentLength((int)fileLen);
String contentDisposition = "attachment;";
m_response.setHeader("Content-Disposition", String.valueOf((new StringBuffer(String.valueOf(contentDisposition))).append(" filename=").append(filename + filetype)));
while((long)totalRead < fileLen)
{
readBytes = fileIn.read(b, 0, 65000);
totalRead += readBytes;
m_response.getOutputStream().write(b, 0, readBytes);
}
fileIn.close();
}
}
//******************************************
when choose "save" ,always HTML page(fileDownLoad.htm) isdo
wnloaded.
but when choose "open" , it is right,import_error_list.txt is opened in the HTML page.
why?ido
nnot know.
else
if 11O line is so : m_response.setContentType("application/zip");
then
: first when choose "save" , HTML page(fileDownLoad.htm) isdo
wnloaded.
if select open then
message windows change to "E050_DownLoad isdo
wnload" be asked;
if select save then
right result: import_error_list.txt can bedo
wnloaded;

How should Ido
?
help.
than
 
Why not just put a link in the html file,
such as <a href="import_error_list.txt"></a>
If you want a dynamic file name, then
user JSP/Servlet to generate it.
<a href="<%=sourceFilePathName%>"></a>
 
你只需要修改两处地方:
response.setContentType("application/octet-stream");
//换一种contentType试试
response.setHeader("Content-Disposition", "attachment;
filename=/"你要在保存窗口中显示的保存文件名/"");
//上面这一句,你写的太复杂啦,(String.valueOf((new StringBuffer)过于复杂了。
应该就可以了。
 
===下载部分===
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", m_contentDisposition);
InputStream in= new FileInputStream(file);
OutputStream out = response.getOutputStream();
int c = in.read();
while(c != -1){
out.write(c);
c = in.read();
}
 
1.probably,because of safety,server maybe be set some limit to make user not to see or call some file. so it is not well to use a link.
and the filepath maybe only be put under such as "http://123.34.45.60/....".(?)
2.ido
not well know the "ContentType" and HTTP Header else
's properties,but "response.setContentType("application/octet-stream");
" yetdo
es not
work.
3.i thank that writing to stream has not question. because can get data at client.
 
参考
http://www.java-cn.com/
 
后退
顶部