我写了一个 servlet(扩展名为 .groovy),它应该以 Base64 编码返回 xml
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String fileName=(String) request.getParameter("fileName")
if (fileName == null || fileName.equals(""))throw new ServletException("Invalid or non-existent file parameter in SendXml servlet.")
if (fileName.indexOf(".xml") == -1)fileName = fileName + ".xml"
System.out.println(fileName)
try {
String relativeWebPath = "/WEB-INF/classes/com/abc/csm/xml/"+fileName
String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath)
String fileContents=new File(absoluteDiskPath).text
response.setContentType("text/xml")
response.addHeader("Content-Disposition", "attachment filename="+ fileName)
XmlHandler xm=new XmlHandler()
PrintWriter out = response.getWriter()
String enxml=xm.encodeBase64(fileContents)
response.setContentLength((int) enxml.length)
out.println(enxml)
out.close()
out.flush()
} catch (Exception e) { println e }
}
XmlHandler encodeBase64 方法
def encodeBase64(String text) {
return new String(Base64.encodeBase64(text.getBytes()))
}
但我想有些东西不见了。请帮忙
更新
另外请评论从包中访问文件的正确方法是什么?