前台中jquery中的一个方法怎么调用下面这个java中的方法?
@Controller
@RequestMapping("/declaration")
public class DeclarationController extends BaseController {
@RequestMapping(params = "downloads")
public void downloads(HttpServletRequest request,HttpServletResponse response,String id) throws IOException{
//从数据库中获取文件 S
AttachmentEntity attachment = etpAutoService.get(AttachmentEntity.class, id);
if (attachment == null) {
return;
}
String downLoadPath = attachment.getMainPath() + attachment.getPath();
String realName = attachment.getName();
File srcFile = new File(downLoadPath);
//从数据库中获取文件 E
ResponseUtils.write(request, response, srcFile, realName);
}
}
------解决思路----------------------
@RequestMapping("/download") public String download(String fileName, HttpServletRequest request, HttpServletResponse response) { response.setCharacterEncoding("utf-8"); response.setContentType("multipart/form-data"); response.setHeader("Content-Disposition", "attachment;fileName=" + fileName); try { String path = Thread.currentThread().getContextClassLoader() .getResource("").getPath() + "download";//这个download目录为啥建立在classes下的 InputStream inputStream = new FileInputStream(new File(path + File.separator + fileName)); OutputStream os = response.getOutputStream(); byte[] b = new byte[2048]; int length; while ((length = inputStream.read(b)) > 0) { os.write(b, 0, length); } // 这里主要关闭。 os.close(); inputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // 返回值要注意,要不然就出现下面这句错误! //java+getOutputStream() has already been called for this response return null; }}
<a href="file/download.do?fileName=map.txt">下载1 </a><br />
------解决思路----------------------
你想调用的话,不说你功能,前台调用的后台的方式都是请求的方式,你只要请求到后台就行了,不管你用ajax或者还是href还是什么。