主题:得到相对路径
我做的是上传 可是不是相对的路径 能传上去之后只能在本机上看到图片 在别的电脑上就看不到图片
请高手帮忙指教修改一下
public ActionForward upLoad(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String path="error";
UpFileForm upForm = (UpFileForm) form;
FormFile file =upForm.getFormFile();
String oldName = file.getFileName();
String newName = Tool.getName()+"."+oldName.substring(oldName.lastIndexOf(".")+1,oldName.length());
String ipath=(super.getServlet().getServletContext().getRealPath("images\\")+"\\"+newName);[color=FF0000]//想在这里得到一下相对路径(目前是绝对路径)[/color]
//System.out.println(ipath);
try {
InputStream strm = file.getInputStream();
FileOutputStream fos = new FileOutputStream(ipath);
int byt = 0;
byte b[] = new byte[8192];
while ((byt = strm.read(b,0,8192)) != -1) {
fos.write(b,0,byt);
}
fos.flush();
fos.close();
strm.close();
request.setAttribute("ipath", ipath);
path = "success";
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mapping.findForward(path);
}
请高手帮忙指教修改一下
public ActionForward upLoad(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String path="error";
UpFileForm upForm = (UpFileForm) form;
FormFile file =upForm.getFormFile();
String oldName = file.getFileName();
String newName = Tool.getName()+"."+oldName.substring(oldName.lastIndexOf(".")+1,oldName.length());
String ipath=(super.getServlet().getServletContext().getRealPath("images\\")+"\\"+newName);[color=FF0000]//想在这里得到一下相对路径(目前是绝对路径)[/color]
//System.out.println(ipath);
try {
InputStream strm = file.getInputStream();
FileOutputStream fos = new FileOutputStream(ipath);
int byt = 0;
byte b[] = new byte[8192];
while ((byt = strm.read(b,0,8192)) != -1) {
fos.write(b,0,byt);
}
fos.flush();
fos.close();
strm.close();
request.setAttribute("ipath", ipath);
path = "success";
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mapping.findForward(path);
}