最近在看一个图片上传的功能,想模仿实现 音乐播放器的后台音乐上传,但是 upLoadPicture()这段代码看的不是很懂,能不能稍微详细点解释一下里面的每句话和实现思想,如果我要做音乐上传的功能思路也和这个一样吗?
@Controller("PictureAction")
@Scope("prototype")
public class PictureAction extends ActionSupport {
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<Picture> picList;
public List<Picture> getPicList() {
return picList;
}
public void setPicList(List<Picture> 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";
}