前端框架选型是企业提升开发效率与用户体验的关键因素
1535
2022-10-28
使用SpringMVC接收文件流上传和表单参数
目录接收文件流上传和表单参数java服务端代码HTML页面代码SpringMVC接收文件上传,并对文件做处理springmvc配置controller代码如下
接收文件流上传和表单参数
在SpringMVC中,接收文件流非常简单,我们可以写个接口用来接收一些文件,同时还可以接收表单参数。
代码参考如下:
JAVA服务端代码
/**
* 接收文件流
*
* @param request 请求
* @return OK
*/
@RequestMapping(value = "/receive/file", method = POST)
public String receiveFile(HttpServletRequest request) {
// 转换为 MultipartHttpServletRequest
if (request instanceof MultipartHttpServletRequest) {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
// 通过表单中的参数名来接收文件流(可用 file.getInputStream() 来接收输入流)
MultipartFile file = multipartRequest.getFile("file");
System.out.println("上传的文件名称:" + file.getOriginalFilename());
System.out.println("上传的文件大小:" + file.getSize());
// 接收其他表单参数
String name = multipartRequest.getParameter("name");
String content = multipartRequest.getParameter("content");
System.out.println("name:" + name);
System.out.println("content:" + content);
return "OK";
} else {
return "不是 MultipartHttpServletRequest";
}
}
HTML页面代码
SpringMVC接收文件上传,并对文件做处理
http client版本4.1.1spring版本3.2.11
在这个例子里面我接收了文件,并转发给另一个机器,其实接收的时候文件的流已经拿到了,想保存到本地,或者对文件分析,自己可以斟酌。
springmvc配置
xmlns:context="http://springframework.org/schema/context" xmlns:mvc="http://springframework.org/schema/mvc" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc.xsd"> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> ehodr
xmlns:context="http://springframework.org/schema/context"
xmlns:mvc="http://springframework.org/schema/mvc"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context.xsd
http://springframework.org/schema/mvc
http://springframework.org/schema/mvc/spring-mvc.xsd">
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
ehodr
controller代码如下
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
@ResponseBody
public String uploadFile(HttpServletRequest request) throws Exception {
FileOutputStream fos = null;
InputStream in = null;
String fileUrl = null;
try {
//将当前上下文初始化给 CommonsMutipartResolver (多部分解析器)
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
//检查form中是否有enctype="multipart/form-data"
if (multipartResolver.isMultipart(request)) {
//将request变成多部分request
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
//获取multiRequest 中所有的文件名
Iterator iterator = multiRequest.getFileNames();
while (iterator.hasNext()) {
MultipartFile multipartFile = multiRequest.getFile(iterator.next().toString());
ehodr in = multipartFile.getInputStream();
File file = new File(multipartFile.getOriginalFilename());
fos = new FileOutputStream(file);
byte[] buff = new byte[1024];
int len = 0;
while ((len = in.read(buff)) > 0) {
fos.write(buff, 0, len);
}
String uploadUrl = platformHttpsDomain + "/uploadFile";
HttpPost post = new HttpPost(uploadUrl);
HttpClient httpclient = new DefaultHttpClient();
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("file", new FileBody(file));
post.setEntity(reqEntity);
HttpResponse response = httpclient.execute(post);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
fileUrl = EntityUtils.toString(response.getEntity());
}
file.deleteOnExit();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
}
return fileUrl;
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~