上传路径的问题

上传路径分为不同的系统有不同的路径,windows会分区,CDEF盘,而mac只有一个,这就导致路径设置的问题。

如果将其设为绝对路径,那么如果将项目部署到了不同系统的机器中则会出现问题。

比如说,A文件存到D盘下,D:/upload

而这个时候mac的机器根本就不能识别,它没有这盘。

怎么解决这个问题?

路径设置的时候不要设置的太完全。不要把什么什么盘的名字加上去。设置路径为文件的名字就行了。

取的时候就将它的父目录给传过去就行了。这样同样也保证了一定的安全性。

save和upload的方法:

@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在前台页面:

<input type = "hidden" id = "dirPath" name= "dirPath" />

<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;

function 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=..."

	/**
	 * 取图片
	 * @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();
    }

存取图片时都要设置图片的上级目录。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值