本文整理匯總了Java中org.apache.commons.fileupload.FileUpload.setHeaderEncoding方法的典型用法代碼示例。如果您正苦於以下問題:Java FileUpload.setHeaderEncoding方法的具體用法?Java FileUpload.setHeaderEncoding怎麽用?Java FileUpload.setHeaderEncoding使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.fileupload.FileUpload
的用法示例。
在下文中一共展示了FileUpload.setHeaderEncoding方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: prepareFileUpload
import org.apache.commons.fileupload.FileUpload; //導入方法依賴的package包/類
/**
* Determine an appropriate FileUpload instance for the given encoding.
* <p>Default implementation returns the shared FileUpload instance
* if the encoding matches, else creates a new FileUpload instance
* with the same configuration other than the desired encoding.
* @param encoding the character encoding to use
* @return an appropriate FileUpload instance.
*/
protected FileUpload prepareFileUpload(String encoding) {
FileUpload fileUpload = getFileUpload();
FileUpload actualFileUpload = fileUpload;
// Use new temporary FileUpload instance if the request specifies
// its own encoding that does not match the default encoding.
if (encoding != null && !encoding.equals(fileUpload.getHeaderEncoding())) {
actualFileUpload = newFileUpload(getFileItemFactory());
actualFileUpload.setSizeMax(fileUpload.getSizeMax());
actualFileUpload.setFileSizeMax(fileUpload.getFileSizeMax());
actualFileUpload.setHeaderEncoding(encoding);
}
return actualFileUpload;
}
示例2: prepareFileUpload
import org.apache.commons.fileupload.FileUpload; //導入方法依賴的package包/類
/**
* Determine an appropriate FileUpload instance for the given encoding.
* <p>Default implementation returns the shared FileUpload instance
* if the encoding matches, else creates a new FileUpload instance
* with the same configuration other than the desired encoding.
* @param encoding the character encoding to use
* @return an appropriate FileUpload instance.
*/
protected FileUpload prepareFileUpload(String encoding) {
FileUpload fileUpload = getFileUpload();
FileUpload actualFileUpload = fileUpload;
// Use new temporary FileUpload instance if the request specifies
// its own encoding that does not match the default encoding.
if (encoding != null && !encoding.equals(fileUpload.getHeaderEncoding())) {
actualFileUpload = newFileUpload(getFileItemFactory());
actualFileUpload.setSizeMax(fileUpload.getSizeMax());
actualFileUpload.setHeaderEncoding(encoding);
}
return actualFileUpload;
}
示例3: uploadFiles
import org.apache.commons.fileupload.FileUpload; //導入方法依賴的package包/類
/**
* @see http://www.oschina.net/code/snippet_698737_13402
* @param request
* @return
* @throws IOException
*/
public static Map<String, Object> uploadFiles(HttpServlet servlet, HttpServletRequest request) {
Map<String, Object> map = JwUtils.newHashMap();
Map<String, String> fileMap = JwUtils.newHashMap();
map.put("file", fileMap);
DiskFileItemFactory factory = new DiskFileItemFactory();// 創建工廠
factory.setSizeThreshold(1024 * 1024 * 30);// 設置最大緩衝區為30M
// 設置緩衝區目錄
String savePath = servlet.getServletContext().getRealPath("/WEB-INF/temp");
factory.setRepository(new File(savePath));
FileUpload upload = new FileUpload(factory);// 獲得上傳解析器
upload.setHeaderEncoding("UTF-8");// 解決上傳文件名亂碼
try {
String targetFolderPath = servlet.getServletContext().getRealPath("/WEB-INF/" + ConfigUtils.getProperty("web.files.upload.folder"));
File targetFolder = new File(targetFolderPath);// 目標文件夾
if(!targetFolder.exists()) {
targetFolder.mkdir();
}
List<FileItem> fileItems = upload.parseRequest(new ServletRequestContext(request));// 解析請求體
for (FileItem fileItem : fileItems) {
if (fileItem.isFormField()) {// 判斷是普通表單項還是文件上傳項
String name = fileItem.getFieldName();// 表單名
String value = fileItem.getString("UTF-8");// 表單值
map.put(name, value);
} else {// 文件上傳項
String fileName = fileItem.getName();// 獲取文件名
if (StringUtils.isEmpty(fileName))// 判斷是否上傳了文件
continue;
// 截取文件名
int index = fileName.lastIndexOf("/");
if (index > -1) {
fileName = fileName.substring(index);
}
// 檢查文件是否允許上傳
index = fileName.lastIndexOf(".");
if (index > -1 && index < fileName.length() - 1) {
String ext = fileName.substring(index + 1).toLowerCase();
if (!ConfigUtils.getString("web.files.upload.extension").contains(";" + ext + ";")) {
LOGGER.warn("The file {} is not allowed to upload.", fileName);
continue;
}
}
// 生成唯一文件名,保留原文件名
String newFileName = UUID.randomUUID().toString();
// 將文件內容寫到服務器端
String targetPath = targetFolderPath + "/" + newFileName;
File targetFile = new File(targetPath);// 目標文件
targetFile.createNewFile();
fileItem.write(targetFile);
fileItem.delete();// 刪除臨時文件
fileMap.put(fileName, newFileName);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}