上传路径分为不同的系统有不同的路径,windows会分区,CDEF盘,而mac只有一个,这就导致路径设置的问题。
如果将其设为绝对路径,那么如果将项目部署到了不同系统的机器中则会出现问题。
比如说,A文件存到D盘下,D:/upload
而这个时候mac的机器根本就不能识别,它没有这盘。
怎么解决这个问题?
路径设置的时候不要设置的太完全。不要把什么什么盘的名字加上去。设置路径为文件的名字就行了。
取的时候就将它的父目录给传过去就行了。这样同样也保证了一定的安全性。
save和upload的方法:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42@RequestMapping(method = RequestMethod.POST) public String save(Book book,HttpServletRequest request){ if(book.getUsers()!=null){ book.getUsers().add(getCurrentUser(request)); }else{ List<User> users = Lists.newArrayList(getCurrentUser(request)); book.setUsers(users); } //String dirPath = book.getName(); //book.setDirPath(dirPath); // if(!dirPath.isEmpty()&dirPath!=null){ // dirPath = dirPath.split(request.getContextPath()+"/",0)[1]; // book.setDirPath(dirPath); //} bookService.save(book); return "redirect:/book"; } /** * 上传课件 * * @param Filedata * @param request * @param fold * @return * @throws IOException */ @RequestMapping(value = "/upload", method = RequestMethod.POST, produces = "text/html;charset=UTF-8") @ResponseBody public String upload(MultipartFile Filedata, HttpServletRequest request, String fold) throws IOException { //String path = "/image?path=" + Constants.UPLOAD_IMAGE +"/"+ fold; String fileName = Filedata.getOriginalFilename(); //String path = "/image?path=" + Constants.UPLOAD_IMAGE +"/"+ fileName; //利用filename和Constants.UPLOAD_IMAGE组成一个路径 String realPath = Constants.UPLOAD_IMAGE +"/"+ fileName; //存文件 FileUtils.copyInputStreamToFile(Filedata.getInputStream(), new File(realPath)); //注意,这里返回的是fileName,而不是路径,因为如果放路径,就有一定安全性,及通用性差 return fileName; }
组织上传路径,然后利用FileUtils上传。
save的时候save book的全属性。book俩重要属性name,dirPath,dirPath在前台页面:
复制代码
1<input type = "hidden" id = "dirPath" name= "dirPath" />
复制代码
1
2
3
4
5
6
7
8
9
10<tr> <th>选择封面插图:</th> <td><input type="file" name="uploadify" id="uploadify" /> <h6>*只支持单个图片上传</h6> <div id="fileQueue"></div></td> </tr> <tr> <th>书籍名称:</th> <td><input type = "text" id="name" name = "name" placeholder="书籍名称" validate="required" validate-message="不能为空!" class="form-control"/></td> </tr>
还是昨天那个uoloadify插件。
upload,js 35行,设置adjunctPath+=data;
复制代码
1
2
3
4
5
6
7
8function onSelect(file){ var fileName=file.name; //fileName=fileName.substring(0,fileName.lastIndexOf(".")); //给id="name"注入值,为fileName //alert(fileName); $("#name").val(fileName); $("#dirPath").val(file.name); }
设置dirPath了。
然后是显示图片:显示图片src="image?path=..."
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31/** * 取图片 * @param response * @param path * @throws IOException */ @RequestMapping(params = {"path"},method = RequestMethod.GET) public void getImage(HttpServletResponse response,String path) throws IOException { //这里将路径组织一下,组织成上传的路径 path = Constants.UPLOAD_IMAGE+"/"+path; File file = new File(path); getImage(file, response); } public void getImage(File file,HttpServletResponse response) throws IOException { FileInputStream inputStream = new FileInputStream(file); byte[] data = new byte[(int)file.length()]; inputStream.read(data); inputStream.close(); String[] f = file.getPath().split("\."); String contentType = f[f.length-1].toLowerCase(); //contentType = MIMEPropertiesReader.getProperty(contentType); response.setContentType(contentType); OutputStream stream = response.getOutputStream(); stream.write(data); stream.flush(); stream.close(); }
存取图片时都要设置图片的上级目录。
最后
以上就是风中巨人最近收集整理的关于上传路径的问题的全部内容,更多相关上传路径内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复