자바 업로드 그림,base 64 방식 및 파일 저장 형식

4928 단어 springbootJava전단
1.전단 용 boottstrap+fileinput
2.백 스테이지 용 spring boot
//   js



//  css
    
    
    
// html
// js  
// base64 qrcode    ,     file    
//     
var qrcodeBase = "";
$("#selectImg").fileinput({
        language : 'zh',
        showUpload:false,
        autoOrientImage:false,
				// uploadUrl: 'http://127.0.0.1:8080/mall/member/uploadImg/base64',//       ,base64
				uploadUrl: 'http://127.0.0.1:8080/mall/member/uploadImg',//     
        allowedFileType : ['image'],
        allowedFileExtensions : ['jpg','jpeg','png'],
        previewFileType :'image',
				showUpload: true,//        
				showRemove: false,//        
				showClose: true,//        
        showCaption: true,
        overwriteInitial: false,
        initialCaption: "       ",
        dropZoneEnabled: false,
				maxFileCount: 1,
				maxFileSize : 1000000, // 1 MB
				enctype: 'multipart/form-data',
				uploadAsync:true,
				uploadExtraData:function(){//       
					var data={
						qrcode:qrcodeBase,
					};
					return data; 
				},
				messages : {
				    acceptFileTypes : '       ',
				    maxFileSize : '      1MB',
				},
				layoutTemplates: {
				  actionUpload: '',//                 
				  actionDelete: ''//      
				},
    }).on("fileselect", function(e, files) {
        var file = this.files[0];
        var reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = function (event) {
			qrcodeBase = event.target.result
        }
    }).on('filecleared', function(event) {//          
			qrcodeBase = "";
	});
//java    ,    
    @PostMapping("/uploadImg")
    public ResponseEntity uoloadImg(HttpServletRequest request,
                                            @RequestParam("file") MultipartFile file){
        Map result = new HashMap();
        String path = null;
        if (file.isEmpty()) {
            result.put("msg", "    ");
            return new ResponseEntity<>(result, HttpStatus.BAD_REQUEST);
        }
        String fileName = file.getOriginalFilename();  //    
        String suffixName = fileName.substring(fileName.lastIndexOf("."));  //    
        //       ,       D://worksapce//upload//
        String filePath = request.getServletContext().getRealPath("") + "upload" + File.separator;
        fileName = UUID.randomUUID() + suffixName; //     
        File dest = new File(filePath + fileName);
        path = filePath+fileName;
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
            result.put("msg","      ");
            return new ResponseEntity<>(result, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        result.put("msg","      ");
        return new ResponseEntity<>(result, HttpStatus.OK);
    }
// java  , base64        
@PostMapping("/uploadImg/base64")
    public ResponseEntity uoloadImg(HttpServletRequest request, 
                                         HttpServletResponse response,
                                         String qrcode){
        Map result = new HashMap();
        try {
            Member member = new Member();
            member.setFldName("  ");
            member.setFldAge("20");
            member.setFldImg(qrcode);
            if (StringUtils.isNotEmpty(qrcode)) {
                service.save(member);
            }
            result.put("msg", "      ");
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (Exception e){
            result.put("msg", "        ");
            return new ResponseEntity<>(result, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

좋은 웹페이지 즐겨찾기