1. 程式人生 > >spring boot 在fastdfs檔案上傳大小限制

spring boot 在fastdfs檔案上傳大小限制

因圖片上傳只能上傳1M大小的圖片,通過度娘獲得更改Nginx中的限制,但是更改沒有獲得預期結果,繼續查詢可以改變MultipartFile 的限制:

修改啟動類


上傳程式碼:


工具類:

package com.steward.commons;                                                                                                                                                          
                                                                                                                                                                                      
import com.github.tobato.fastdfs.domain.StorePath;                                                                                                                                    
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;                                                                                                           
import com.github.tobato.fastdfs.service.FastFileStorageClient;                                                                                                                       
import org.apache.commons.io.FilenameUtils;                                                                                                                                           
import org.apache.commons.lang3.StringUtils;                                                                                                                                          
import org.slf4j.Logger;                                                                                                                                                              
import org.slf4j.LoggerFactory;                                                                                                                                                       
import org.springframework.beans.factory.annotation.Autowired;                                                                                                                        
import org.springframework.stereotype.Component;                                                                                                                                      
import org.springframework.web.multipart.MultipartFile;                                                                                                                               
                                                                                                                                                                                      
import java.io.ByteArrayInputStream;                                                                                                                                                  
import java.io.IOException;                                                                                                                                                           
import java.nio.charset.Charset;                                                                                                                                                      
                                                                                                                                                                                      
/**                                                                                                                                                                                   
 * <p>                                                                                                                                                                                
 * Description: FastDFS檔案上傳下載包裝類                                                                                                                                                      
 * </p>                                                                                                                                                                               
 * <p>                                                                                                                                                                                
 * Copyright: Copyright (c) 2016                                                                                                                                                      
 * </p>                                                                                                                                                                               
 */                                                                                                                                                                                   
@Component                                                                                                                                                                            
public class FastDFSClientWrapper {                                                                                                                                                   
                                                                                                                                                                                      
private final Logger logger = LoggerFactory.getLogger(FastDFSClientWrapper.class);                                                                                                
                                                                                                                                                                                      
@Autowired                                                                                                                                                                        
private FastFileStorageClient storageClient;                                                                                                                                      
                                                                                                                                                                                      
/**                                                                                                                                                                               
* 上傳檔案                                                                                                                                                                           
*                                                                                                                                                                                
* @param file                                                                                                                                                                    
*            檔案物件                                                                                                                                                                
* @return 檔案訪問地址                                                                                                                                                                 
* @throws IOException                                                                                                                                                            
*/                                                                                                                                                                               
public String uploadFile(MultipartFile file) throws IOException {                                                                                                                 
StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),                                                                                         
FilenameUtils.getExtension(file.getOriginalFilename()), null);                                                                                                        
return getResAccessUrl(storePath);                                                                                                                                            
}                                                                                                                                                                                 
                                                                                                                                                                                      
/**                                                                                                                                                                               
* 將一段字串生成一個檔案上傳                                                                                                                                                                 
*                                                                                                                                                                                
* @param content                                                                                                                                                                 
*            檔案內容                                                                                                                                                                
* @param fileExtension                                                                                                                                                           
* @return                                                                                                                                                                        
*/                                                                                                                                                                               
public String uploadFile(String content, String fileExtension) {                                                                                                                  
byte[] buff = content.getBytes(Charset.forName("UTF-8"));                                                                                                                     
ByteArrayInputStream stream = new ByteArrayInputStream(buff);                                                                                                                 
StorePath storePath = storageClient.uploadFile(stream, buff.length, fileExtension, null);                                                                                     
return getResAccessUrl(storePath);                                                                                                                                            
}                                                                                                                                                                                 
                                                                                                                                                                                      
// 封裝圖片完整URL地址                                                                                                                                                                    
private String getResAccessUrl(StorePath storePath) {                                                                                                                             
String fileUrl = storePath.getFullPath();                                                                                                                                     
return fileUrl;                                                                                                                                                               
}                                                                                                                                                                                 
                                                                                                                                                                                      
/**                                                                                                                                                                               
* 傳圖片並同時生成一個縮圖 "JPG", "JPEG", "PNG", "GIF", "BMP", "WBMP"                                                                                                                       
*                                                                                                                                                                                
* @param file                                                                                                                                                                    
*            檔案物件                                                                                                                                                                
* @return 檔案訪問地址                                                                                                                                                                 
* @throws IOException                                                                                                                                                            
*/                                                                                                                                                                               
public String uploadImageAndCrtThumbImage(MultipartFile file) throws IOException {                                                                                                
StorePath storePath = storageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(),                                                                        
FilenameUtils.getExtension(file.getOriginalFilename()), null);                                                                                                        
return getResAccessUrl(storePath);                                                                                                                                            
}                                                                                                                                                                                 
                                                                                                                                                                                      
/**                                                                                                                                                                               
* 刪除檔案                                                                                                                                                                           
*                                                                                                                                                                                
* @param fileUrl                                                                                                                                                                 
*            檔案訪問地址                                                                                                                                                              
* @return                                                                                                                                                                        
*/                                                                                                                                                                               
public void deleteFile(String fileUrl) {                                                                                                                                          
if (StringUtils.isEmpty(fileUrl)) {                                                                                                                                           
return;                                                                                                                                                                   
}                                                                                                                                                                             
try {                                                                                                                                                                         
StorePath storePath = StorePath.praseFromUrl(fileUrl);                                                                                                                    
storageClient.deleteFile(storePath.getGroup(), storePath.getPath());                                                                                                      
} catch (FdfsUnsupportStorePathException e) {                                                                                                                                 
logger.warn(e.getMessage());                                                                                                                                              
}                                                                                                                                                                             
}                                                                                                                                                                                 
}