如何实现下载文件后的自动保存 通过一个url把文件下载保存到d盘下如果用java代码实现 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如果是在页面上,那直接<a href="">,看浏览器对文件的支持情况,可以直接在浏览器打开或者下载。如果是在代码层面,那就用httpClient下载文件到你想保存的地方吧 public File fileDownLoad(String filePath) { HttpUriRequest httpUriRequest = new HttpGet(filePath); String upload = TaskServiceImpl.class.getResource("/").toString().replace("file:", ""); upload = upload.substring(0, upload.indexOf("WEB-INF")) + "upload"; File file = new File(upload); if (file.isFile() || !file.exists()) { file.mkdirs(); } file = new File(file + File.separator + fileUrl.substring(filePath.lastIndexOf("/"))); logger.info("下载文件到路径{}下", file.getAbsolutePath()); try { HttpResponse remoteResponse = this.httpClient.execute(httpUriRequest); HttpEntity entity = remoteResponse.getEntity(); InputStream is = entity.getContent(); BufferedOutputStream write = new BufferedOutputStream(new FileOutputStream(file)); int nextChar; while ((nextChar = is.read()) != -1) { write.write(nextChar); } is.close(); write.flush(); write.close(); logger.info("文件下载完成,文件大小为{}", file.length()); return file; } catch (Exception e) { e.printStackTrace(); logger.warn("下载文件失败{}", e.toString()); return null; } }给你个下载的示例吧 怎么获取苹果官网的数据啊! jsp中判断的问题 请教大虾们 怎么在 JSP 页面上点一个按钮 把页面的报表生成 EXCEL 文件,谢谢.在线等.急~~~ jsp数据显示的问题 求java webservice开发的经典例子 初学spring遇到的问题 Java如何将ireport生成的pdf模版保存到本地 请高手指教!! 恳求Jbuilder9的注册码? Cannot create PoolableConnectionFactory 求助 很急 数据库表 新加一个字段 会不会对ssh项目有影响 EXT TREE 数据混乱
如果是在代码层面,那就用httpClient下载文件到你想保存的地方吧
public File fileDownLoad(String filePath)
{
HttpUriRequest httpUriRequest = new HttpGet(filePath); String upload = TaskServiceImpl.class.getResource("/").toString().replace("file:", ""); upload = upload.substring(0, upload.indexOf("WEB-INF")) + "upload"; File file = new File(upload); if (file.isFile() || !file.exists())
{
file.mkdirs();
} file = new File(file + File.separator + fileUrl.substring(filePath.lastIndexOf("/")));
logger.info("下载文件到路径{}下", file.getAbsolutePath());
try
{
HttpResponse remoteResponse = this.httpClient.execute(httpUriRequest); HttpEntity entity = remoteResponse.getEntity(); InputStream is = entity.getContent(); BufferedOutputStream write = new BufferedOutputStream(new FileOutputStream(file)); int nextChar;
while ((nextChar = is.read()) != -1)
{
write.write(nextChar);
} is.close(); write.flush(); write.close(); logger.info("文件下载完成,文件大小为{}", file.length()); return file; } catch (Exception e)
{
e.printStackTrace();
logger.warn("下载文件失败{}", e.toString());
return null;
}
}
给你个下载的示例吧