问题描述
- ssh图片上传与音乐上传
-
最近在看一个图片上传的功能,想模仿实现 音乐播放器的后台音乐上传,但是 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"; }
解决方案
音乐上传思路是一样的;
上面的解释你跟着代码再看看,自己再动动手就差不多了。
如果回答对你有帮助,请采纳
解决方案二:
这是通过Struts2自带的上传组件,上传后的文件名是用UUID加上上传的图片的类型作为上传后保存的文件名,这样可以避免重复!文件上传后保存的位置就是你在项目里建立的那个upload文件夹的绝对路径。
上传视频音乐问价类似,只是可能会在Stuts.xml里进行一定的配置。
时间: 2024-08-03 07:17:56