java org.springframework.web.multipart.MultipartFile 上傳文件等


接口需求是Android/iPhone需要通過接口上傳一些文件,例如圖片,jar包,rar壓縮包,視頻,音樂...等!困擾我這個菜鳥很久,整理一下!!分享給同我一樣的菜鳥 

后台需要處理這些數據,之前沒有接觸過!但類似的FileUpLoad的插件用過,其實原理差不多,因為計算機原始數據都是01代碼,所以只要將這些文件保存成二進制就可以,數據庫(mysql),最大4G!! 
問題1,如果實現!聯調之后共享代碼. 
問題2,為什么要轉為2進制,不直接把上傳的文件放在服務器上! 
我也請教了別人,首先放在服務器上會很亂,不方便維護!管理不方便,優點當然是不占用數據庫資源了(很多時候軟件的瓶頸就是數據庫的優化). 
至於2進制,當然放在數據庫里很方便維護,管理!移植,都只需要SQL語句都可以完成,但有個重點是性能問題(內存的占用),如果使用Hibernate的話,需要用懶加載,這樣可以保證性能不會很操蛋. 
select * from table where id=1 
from table where id=1 
貌似兩個功能是一樣的,但是用了懶加載第二個也許在加載數據更快,至於為什么去了解一下懶加載吧!! 
問題3,如何選擇呢! 
如果經常需要訪問這個資源,那就直接用文件的形式,緩存資源!!如果不是經常使用可以用二進制.如果你有不同看法,請賜教!! 

public static void main(String[] args) throws Exception {
		/**
		 * 將字符串轉換為二進制
		 */
		String str1 = "我是趙昌文";				//需要轉換的字符串
		byte[] bys = str1.getBytes("UTF-8");	//將字符串轉換為byte數組(GBK...等)
		//用來保存UTF-8的byte數據
		StringBuffer sb = new StringBuffer();
		//用來保存二進制
		StringBuffer sb1 = new StringBuffer();
		
		//遍歷byte的數據(跟轉換無關)
		for(int j=0;j<bys.length;j++){
			//(j+1)這樣在第二次循環j從1開始,j本身不參與("運算"---字符串拼接),可以寫j+1試試
			//System.out.println("byte(UTF-8)第"+(j+1)+"個"+bys[j]+"轉為二進制"+Integer.toBinaryString(bys[j]));		
			sb.append(bys[j]+",");
			sb1.append(Integer.toBinaryString(bys[j])+",");
		}
		/**
		 * 將二進制轉換回字符串
		 */
		System.out.println("UTF-8:"+sb);
		System.out.println("二進制:"+sb1);
		
//		byte[] b = {-26,-120,-111};
//		System.out.println("轉換回來!!"+new String(b,"UTF-8"));	
	}

上面是自己寫的一些轉換,文件轉換二進制然后才保存,下邊是上傳!

//提交資源保存
    @RequestMapping("/saveResource.aa")
    public ModelAndView saveResource(@RequestParam("fileParameter") MultipartFile file){
        map = new HashMap();
        //為false時文件不為空
        if(file.getName()!=null&&file.isEmpty()==false){
            ResourceImg resource = new ResourceImg();            
            try {
                resource.setBody(Hibernate.createBlob(file.getBytes()));
                resource.setMimeType(file.getContentType());
                resource.setName(file.getName());
                map.put("result",rm.saveResource(resource));
            } catch (IOException e) {
                e.printStackTrace();
            }
            return new ModelAndView("jsonView",map);
        }else{
            map.put("result","null");
            return new ModelAndView("jsonView",map);
        }
    }

 

//保存到路徑,跟上邊的區別,上邊的保存數據庫

//將圖片存到服務器
    @RequestMapping("/imageUpload.aa")
    public String processImageUpload(@RequestParam("fileParameter") MultipartFile image) throws IOException {
        FileCopyUtils.copy(image.getBytes(),new File("d:/"+image.getOriginalFilename()));
        return "imageList";
   }

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



猜您在找 Spring使用mutipartFile上傳文件報錯【Failed to instantiate [org.springframework.web.multipart.MultipartFile]】 springmvc上傳文件報錯org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.web.multipart.MultipartFile] SpringMVC使用MultipartFile上傳文件報錯【org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.multipart.MultipartFile]: Specified class is an interface】 Java之——java.lang.NoSuchMethodException: [org.springframework.web.multipart.MultipartFile;.() spring boot 上傳文件出錯:org.springframework.web.multipart.MultipartException: Could not parse multipart s spring boot 線上故障 上傳文件出錯:org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; 上傳文件Request processing failed;nested exception is org.springframework.web.multipart.MultipartException:Failed to parse multipart servlet request;multipart/form-data request failed.(**沒有權限**) SpringMVC文件上傳報錯org.apache.catalina.connector.RequestFacade cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest springboot框架,文件上傳問題===org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'multipart/form-data; 文件上傳出現:Failed to introspect Class [org.springframework.web.multipart.commons.CommonsMultipartResolver] from ClassLoader [ParallelWebappClassLoade
 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM