SSH上传下载的小demo

来源:互联网 发布:苹果特效相机软件 编辑:程序博客网 时间:2024/05/16 18:04
SSH上传图片
private File photos;
private String photosFileName;
get  set 方法
public String add() throws Exception{
String path = ServletActionContext.getServletContext().getRealPath("/images");
FileUtils.copyFile(photos, new File(path,photosFileName));
person.setPhoto(photosFileName);
personService.saveProduct(person);
return "add";
}
下载图片
//获取文件名
private String fileName;
//输入流
private InputStream downloadFile;
public String download() throws Exception{
fileName = person.getPhoto();
String path = ServletActionContext.getServletContext().getRealPath("/images/"+fileName);
downloadFile = new FileInputStream(path);
File file = new File(path);
photosFileName = file.getName();
//fileName = FilenameUtils.getName(path);
photosFileName = URLEncoder.encode(photosFileName, "utf-8");
                    return "download";
}
下载需要在Struts中配置result
<result name="download" type="stream">
<!-- 下载文件的类型 -->
<param name="contentType">image/png</param>
        <!-- 设置下载的处理方式 -->
        <param name="contentDisposition">attachment;fileName="${photosFileName}"
        </param>
<!-- 设置下载的流的属性名 -->
<param name="inputName">downloadFile</param>
</result>
原创粉丝点击