如题,我照着书上的例子写的,不一直出错,提示“系统找不到指定路径”。
- Java code
private File doc; private String docContentType; private String docFileName; private String path; public String execute() throws Exception{ docFileName=getFileName(docFileName); System.out.println(docFileName); FileOutputStream fos = new FileOutputStream(getPath()+"\\"+docFileName); //FileOutputStream fos = new FileOutputStream(ServletActionContext.getServletContext().getRealPath(getPath())+"\\"+docFileName); FileInputStream fis=new FileInputStream(doc); byte[] b=new byte[1024]; int length=0; while((length=fis.read(b))>0){ fos.write(b,0,length); } return SUCCESS; } public String getFileName(String fileName){ int position = fileName.lastIndexOf("."); String extension=fileName.substring(position); return System.currentTimeMillis()+extension; }
这是action的主要代码。struts.xml的配置如下:
- XML code
<action name="myUpload" class="action.MyUpload"> <param name="path">/upload</param> <result name="success">/fileupload/uploadSuccess.jsp</result> <result name="input">/fileupload/upload.jsp</result> </action>
在项目的根路径下我已经新建了upload文件夹了,不过每次运行都提示“系统找不到指定路径”的错误。如果我把相对路径换成绝对路径就可以上传。请问一下用相对路径改怎么改?
------解决方案--------------------
../代表上一级文件夹
文件名字/代表下一级文件夹
输出下这句话自己对路径getPath()+"\\"+docFileName
------解决方案--------------------
图片显示应该用这个一路径
String path = request.getScheme() + "://" + request.getServerName()
+ ":" + request.getServerPort()
+ "\\项目名\\upload\\";
然后在加上upload文件夹里面的图片文件
------解决方案--------------------
path要用request.getSession().getServletContext().getRealPath("/upload")
------解决方案--------------------
ServletActionContext.getServletContext().getRealPath("upload") ;