cd86254081 2015-07-04 14:22 采纳率: 100%
浏览 1621
已采纳

上传图片和音乐的路径问题

代码如下:

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}"就可以吗。读取音乐的时候是不是一样的方式?

  • 写回答

3条回答 默认 最新

  • 全栈极简 博客专家认证 2015-07-04 14:28
    关注

    realpath是实际路径,文件上传时需要这个完整的路径才能知道传到哪。
    imagePath这个只是保存到数据库的路径,由于是服务器端,我们只要知道在当前站点根目录下的路径即可。由于是相对路径,项目中引用图片显示的时候,可以直接用这个路径。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测