JSP的上传获得不到filepath

input[type=file]真的没有办法获取所选文件的绝对路径吗?
是的 安全考虑 浏览器无法获取 文件绝对地址
但改良的方法但不万能
<input type="file" ></input>
如何获取该标签选中的文件的绝对路径?
1. var getFullPath = function(obj) { 
   2.           if (obj) { 
   3.               if (window.navigator.userAgent.indexOf("MSIE") >= 0) { 
   4.                   obj.select(); 
   5.                   //FOR IE选中上传控件中的值 
   6.                   return obj.selection.createRange().text; 
   7.               } 
   8.               else if (window.navigator.userAgent.indexOf("FireFox") >= 0) { 
   9.                   if (obj.files) { 
  10.                       //getAsDataURL()方法 item(0) 
  11.                       //在FireFox中获取file控件中路径 
  12.                       return obj.files.items[0].getAsDataURL(); 
  13.                   } 
  14.                   return obj.value; 
  15.               } 
  16.               return obj.value; 
  17.           } 
  18.       } 


二。可以servlet形式来接收数据后另存到自己定义的地方来解决这个问题
@RequestMapping("/toImportFromExcel")
    public String toImportFromExcel(String targetTypeValue,MultipartFile file){
    addRequestAttribute("targetTypeValue", targetTypeValue);
    String dietaryfilesPath = "/excel";
    String uuidFileName = FileUtils.genUuidFileNameByFileName(file.getOriginalFilename());
    String filepath = String.format("%1$s%2$s/%3$s", ConfigUtils.getSysConfig().getAttachmentPath(),dietaryfilesPath,uuidFileName);        
        try {
FileUtils.saveFile(file.getBytes(), filepath);
} catch (IOException e) {
throw new BusinessException(RespCode.RES_999);
}
       
        List<String> list = new ArrayList<String>();
        List list1 = new ArrayList<>();
        try {
        if(StringUtils.isNotEmpty(filepath)){
        list = ExcelUtil.exportListFromExcel(new File(filepath), 0); 
        }

猜你喜欢

转载自weitao1026.iteye.com/blog/2261255