日期:2014-05-19  浏览次数:20659 次

struts2文件上传路径问题
action的changePhoto()函数处理文件上传,其中savePath已经在struts.xml中指定好了
public String changePhoto()throws Exception{
 FileInputStream is = new FileInputStream(file);
 File destFile = new File(savePath,fileFileName);
 FileOutputStream os = new FileOutputStream(destFile);
 byte[] buffer = new byte[1024];
 int length = 0;
 while((length = is.read(buffer)) > 0){
 os.write(buffer,0,length);
 }
 is.close();
 os.close();
 return SUCCESS;
 }

我在struts.xml中把savePath设置为"\photo"  
<param name="savePath">/photo</param>
action中对应的语句为
public String getSavePath()throws Exception{
 return ServletActionContext.getServletContext().getRealPath(savePath);
 }
 public void setSavePath(String savePath) {
 this.savePath = savePath;
 }
按理应该上传至WebContent文件夹下的photo文件夹中(我的jsp页面也处于WebContent文件夹下) 但是却上传到了我的E盘的根目录下的photo文件夹里

同时我在一个show.jsp页面中有如下语句
<img src="photo/5.jpg" />
其中show.jsp页面与photo文件夹处于同一文件夹下(都处于WebContent文件夹下) 但是页面上显示不出来图片
只有我把绝对路径输入
<img src="E:\a\J2EE\workspace\DemoSNS\WebContent\photo\5.jpg" />
页面才能显示出图片来 实在不理解这是怎么回事

------解决方案--------------------
http://topic.csdn.net/u/20120320/20/eb2c1953-c660-43fd-8e6a-29f75cb5eb23.html