问题描述
解决方案
/**
* 下载方法(在页面上显示不保存在该项目里的图片)
* @param mapping
* @param ctrlForm
* @param request
* @param response
* @return
* @throws Exception
*/
public void showImg(ActionMapping mapping, ActionForm ctrlForm,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Properties Prop = new Properties();
InputStream Input = SysHelpBiz.class.getResourceAsStream("FilePath.properties");
Prop.load(Input);
String sd_path = Prop.getProperty("PATH");
File file = new File(sd_path+File.separator+request.getParameter("path"));
String filename = file.getName();
response.setContentType("application/x-msdownload; charset=utf-8");
response.setContentLength((int) file.length());
response.setHeader("Content-Disposition", "attachment;filename="
+ java.net.URLEncoder.encode(filename, "UTF-8"));
// read file
FileInputStream fis = null;
OutputStream myout = null;
try {
fis = new FileInputStream(file);
BufferedInputStream buff = new BufferedInputStream(fis);
byte[] b = new byte[1024];
long k = 0;
myout = response.getOutputStream();
while (k < file.length())
{
int j = buff.read(b, 0, 1024);
k += j;
myout.write(b, 0, j);
}
myout.flush();
} catch (Exception e) {
throw new RuntimeException("文件下载时出现异常");
} finally {
fis.close();
myout.close();
}
}
页面
<img src='<%=path%>/cjsc.do?method=showImg&path=${p.BMUS_PIC}' width="60px" height="50px"/>
这里的path只是个图片名
仅供参考。
解决方案二:
服务器返回新保存的图片的url地址,然后设置img的src加载图片就行了,多增加一个属性存储新图片地址,如返回
{"success":true,"img":"新图片的路径"}
if(data.success){
if(data.img)$('#showPic1').attr('src',data.img);//如果上传了新图片
//...原来的代码
}