代码如下:
private static final long serialVersionUID = 572146812454L;
private File image; // 上传的文件
private String imageFileName; // 文件名称
private String imageContentType; // 文件类型
User user = (User) ActionContext.getContext().getSession().get("user");
private HttpServletResponse response = ServletActionContext.getResponse();
private HttpServletRequest request = ServletActionContext.getRequest();
private List picList;
public List getPicList() {
return picList;
}
public void setPicList(List picList) {
this.picList = picList;
}
private String picId;
private String userId;
private String commentPic;
//省略getter和setter
public String uploadPicture() throws IOException {
String s = UUID.randomUUID().toString();
String lastName = imageFileName.substring(imageFileName
.lastIndexOf(".") + 1, imageFileName.length());
String name = s + "." + lastName;
username = user.getUsername();
//获取服务器路径
String realpath = ServletActionContext.getServletContext().getRealPath(
"/upload");
System.out.println("realpath: " + realpath);
if (image != null) {
File savefile = new File(new File(realpath), name);
if (!savefile.getParentFile().exists())
savefile.getParentFile().mkdirs();
FileUtils.copyFile(image, savefile);
String imagePath = "/sDemo/upload/" + name;
System.out.println("imagePath: " + imagePath);
Picture picture = new Picture();
picture.setUserId(user.getId());
picture.setUserName(user.getUsername());
picture.setPictureUrl(imagePath);
ser.insertPicture(picture);
}
return "upload";
}
我想知道 String realpath = ServletActionContext.getServletContext().getRealPath("/upload");里"/upload"是干什么的?和 String imagePath = "/sDemo/upload/" + name;里的upload有关系吗?
我用这个上传图片上传了一张图片,但是在imagepath路径下并没有看到图片,是存到了realpath下吗?既然这样为什么要将url=imagepath存入数据库而不是存realpath?
上传的图片是不是实际上存在tomcat上的realpath下,通过imagepath存取?这两个路径之间通过什么建立关系?读取图片的时候读取<img src="${pictureUrl}"就可以吗。读取音乐的时候是不是一样的方式?