您的位置:首页 > 编程语言 > Java开发

spring mvc项目使用form表单上传文件

2016-10-27 11:29 886 查看
因为spring mvc中的解析器已经将form表单中的文件给解析成文件对象,此文件对象可以直接使用

spring-servlet.xml配置文件

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8" />
<property name="maxUploadSize" value="1073741824" />
</bean>


因此直接在controller中使用以下方法接收多个文件或者单个文件

@RequestParam MultipartFile[] myFiles
@RequestParam MultipartFile myFile


以下为spring中,将servlet请求中的文件内容解析过 ,使用FileUpload插件

org.apache.commons.fileupload

/**
* Parse the given servlet request, resolving its multipart elements.
* @param request the request to parse
* @return the parsing result
* @throws MultipartException if multipart resolution failed.
*/
protected MultipartParsingResult parseRequest(HttpServletRequest request) throws MultipartException {
String encoding = determineEncoding(request);
FileUpload fileUpload = prepareFileUpload(encoding);
try {
List<FileItem> fileItems = ((ServletFileUpload) fileUpload).parseRequest(request);
return parseFileItems(fileItems, encoding);
}catch (FileUploadBase.SizeLimitExceededException ex) {
throw new MaxUploadSizeExceededException(fileUpload.getSizeMax(), ex);
}catch (FileUploadException ex) {
throw new MultipartException("Could not parse multipart servlet request", ex);
}
}


以下为使用文件对象上传至服务器

/**
*
* @Description:form表单提交多个文件保存本地
* @param files 多个文件
* @param fileParm 文件参数
* @return 保存成功图片服务器返回结果
* @author zhiwei.yan
* @date 2016年10月24日 下午4:35:25
*/
public static JSONArray saveOfFileupload(MultipartFile[] files, FileParm fileParm) {
final String path = getBaseFilePath();
final JSONArray imgList = new JSONArray();
try {
//多个文件解析
int times = 0;
for (MultipartFile item : files) {
// 如果获取的 表单信息是普通的 文本 信息
if (!item.isEmpty()) {
times++;
// 获取项目路径名
final String value = item.getOriginalFilename();
// 索引到最后一个反斜杠
final int start = value.lastIndexOf(".");
// 截取 上传文件的 字符串名字,加1是 去掉反斜杠,
final String fileSuffix = value.substring(start);
// 临时文件名
final String fileName = System.currentTimeMillis() + fileSuffix;
// 输出流,用于写入本地
final OutputStream out = new FileOutputStream(new File(path, fileName));
// 获取form表达的输入流
final InputStream in = item.getInputStream();

int length = 0;
final byte[] buf = new byte[DEFAULT_BUFFER_SIZE];

// in.read(buf) 每次读到的数据存放在 buf 数组中
while ((length = in.read(buf)) != -1) {
// 在 buf 数组中 取出数据 写到 (输出流)临时文件存放目录
out.write(buf, 0, length);
}
in.close();
out.close();
//保存至图片服务器,此处根据自身图片服务器管理,这段自身调整
final JSONObject json = JSONObject.fromObject(saveFileToServer(path + fileName, fileParm.getRelativePath(),
fileParm.getFileNameUpload() + times, fileParm.getPictureSize(), fileParm.getFileSource(),
fileParm.getUploadUrl()));
final JSONObject result = (JSONObject) JSONArray.fromObject(json.get("result")).get(0);
imgList.add(result);
}
}
return imgList;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**
*
* @Description:取得basepath
* @return
* @author zhiwei.yan
* @date 2016年4月16日 下午5:44:44
*/
public static String getBaseFilePath() {
String fileName = "os.name";
String fileWindows = "WINDOWS";
String basePath = null;
// window,linux下路径不同
final String os = System.getProperty(fileName).toUpperCase();
if (os.contains(fileWindows )) {
basePath = PropUtils.getConfig("WfileBasePath");//此处windows存放路径
} else {
basePath = PropUtils.getConfig("LfileBasePath");//linux存放路径
}
return basePath;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: