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);
}
}
上一篇: 设置Cookie
下一篇: Spring注解@Qualifier用法