好久没有写点东西了,其实自己已经没有什么好写的,技术那些东西,自己还学到的很少很少,经验是不断积累起来的,还有就是靠自己不断的追求吧.
(其实就是上次写导出文件的一个补充罢了,唯一变的地方其实就是一个设定问题,)也就是response.setContentType("application/x-msdownload;charset=UTF-8");
呵呵,我写的都是一些小东西.不过自己还是尽力将之做得更好.其他不说了,直接来程序吧.
Java代码,这个是一个Servlet,其实无所谓,你在页面上直接location.href也可.里面有不少无用代码,不过还是留下吧,呵呵,有些其实也挺有用的.
JSP页面代码
(其实就是上次写导出文件的一个补充罢了,唯一变的地方其实就是一个设定问题,)也就是response.setContentType("application/x-msdownload;charset=UTF-8");
呵呵,我写的都是一些小东西.不过自己还是尽力将之做得更好.其他不说了,直接来程序吧.
Java代码,这个是一个Servlet,其实无所谓,你在页面上直接location.href也可.里面有不少无用代码,不过还是留下吧,呵呵,有些其实也挺有用的.
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.lxh.smart.SmartUpload;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.radiantek.m2m.doc.dao.DownloadDocDao;
public class DownLoadDoc extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public DownLoadDoc() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//重点
response.setContentType("application/x-msdownload;charset=UTF-8");
// 这里得到的是ID,然后再去找.
String FileName = new String(request.getParameter("downName"));
// String FilePath = new String(
// request.getParameter("FilePath").getBytes("iso8859_1") ) ;
String FilePath = request.getSession().getServletContext().getRealPath(
"/");
String appXmlPath = "/com/radiantek/m2m/config/spring/applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
appXmlPath);
DownloadDocDao downloadDocDao = (DownloadDocDao) applicationContext
.getBean("downloadDocDao");
String fileName = downloadDocDao.findByFileName(FileName);
// System.out.println(FilePath);
// @SuppressWarnings("unused")
// DownLoad dl = new DownLoad () ;
try {
// dl.down(response, FilePath, FileName);
// System.out.println(FilePath) ;
SmartUpload smart = new SmartUpload();
smart.initialize(super.getServletConfig(), request, response);
smart.setContentDisposition(null);
FilePath = fileName;
int filen = FilePath.lastIndexOf("\\");
String fName = FilePath.substring(filen+1);
// System.out.println(FilePath);
//重点,最主要的就是这句话了,
smart.downloadFile(FilePath, "application/x-msdownload;charset=UTF-8", java.net.URLEncoder.encode(fName, "UTF-8"));
} catch (Exception e) {
e.printStackTrace();
}
}
public void init() throws ServletException {
}
}
JSP页面代码
....这里就是一个传递参数问题,用到了Struts的一些内容,不管用什么都可以,只要传递参数了.
<input type="button" value=" 下载 " class="button_class"
onclick="down('<s:property value="FILE_ID"/>');">
.....
function down(DOWN_NAME){
location.href = "<%=path%>/servlet/DownLoadDoc?downName=" + DOWN_NAME;
}