欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

servlet实现简单下载(copy)

程序员文章站 2022-03-03 12:40:00
...

public class CopyDataServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String realRoot = this.getServletConfig().getServletContext().getRealPath(
"/")+"/data/";
File theFile = LYFLDAPUtil.readDatFileOfCurDay();
if(null != theFile){
String src = theFile.getName();
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
String dateString = sf.format(new Date());
String dst = realRoot + dateString+".dat";
System.out.println(theFile.getName());
InputStream in = new FileInputStream(LYFLDAPUtil.SAMETIME_FILE_PATH + File.separator +src); //source file
OutputStream out = new FileOutputStream(dst);//destination file

byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}



}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doGet(request, response);
}

}
相关标签: Servlet