最近在看一個圖片上傳的功能,想模仿實現 音樂播放器的後台音樂上傳,但是 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";
}
音樂上傳思路是一樣的;
上面的解釋你跟著代碼再看看,自己再動動手就差不多了。
如果回答對你有幫助,請采納