java web 怎么从服务器下载文件到客户端的指定位置
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java web 怎么从服务器下载文件到客户端的指定位置相关的知识,希望对你有一定的参考价值。
就是从服务器下载文件到客户端的指定的位置
源代码 解决后加分
我脚得的吧,这应该是浏览器考虑的问题吧,比如谷歌浏览器,就有这么一个配置:
/** 新闻代表图片 */
private File newsPicture;//文件字段和jsp 中文件name相同
private String newsPictureFileName;//文件名称,可以自动获取
private String newsPictureContentType;//文件类型,可以自动判断
//上传的共用方法,srcFile源文件,savePath保存的路径,fileName文件名称,你使用这个方法就可以上传了。
public static File uploadUtil(File srcFile,String savePath,String FileName)
InputStream is = null;
OutputStream os = null;
File toFile = null;
if(srcFile!=null)
try
is = new FileInputStream(srcFile);
String fileName = (new Date().getTime())+FileName.substring(FileName.indexOf("."));
toFile = new File(ServletActionContext.getServletContext().getRealPath(savePath), fileName);
os = new FileOutputStream(toFile);
byte[] buffer = new byte[1024];
int length = 0;
while ((length = is.read(buffer)) > 0)
os.write(buffer, 0, length);
os.flush();
catch (FileNotFoundException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
finally
try
is.close();
os.close();
catch (IOException e)
e.printStackTrace();
return toFile;
追问
当加载一个JSP页面的时候从服务器下载文件 到客户端怎么实现
追答下载要稍微麻烦一点:需要配置struts.xml配置流,需要
1、
2、在需要下载的action中
application/octet-stream;charset=ISO8859-1//字符集,必须转换成ISO文件名才正确
attachment;filename="$fileRealName"//下载名为filename中的值
downloadFile
3、下载方法需要该方法
public InputStream getDownloadFile() throws Exception
this.setFileName(fileName);
String name = this.getFileRealName();
String realPath = "/upload/"+name;
InputStream in = ServletActionContext.getServletContext().getResourceAsStream(realPath);
return in;
4、页面需要下载列表
$uploadFiles.filename网上有很多资源,你多看下就能懂
web 程序要在服务端运行,怎么从服务端下载文件到客户端的指定位置
追答写WEB,服务器端是没有权限直接访问客户端的本地资源的———除非客户端的权限全开放了,那写applet可以。
以上是关于java web 怎么从服务器下载文件到客户端的指定位置的主要内容,如果未能解决你的问题,请参考以下文章