Flask接口签名sign原理与实例代码浅析
234
2023-06-17
详解spring mvc(注解)上传文件的简单例子
spring mvc(注解)上传文件的简单例子。
这有几个需要注意的地方
1.form的enctype=”multipart/form-data” 这个是上传文件必须的
2.applicationContext.xml中
3、亲这两个jar包不能少哦,之前就是因为少了这个两个jar,一直报一些奇葩的错,给我累了个半死~(版本没什么要求)
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
大家可以看具体代码如下:
web.xml
/WEB-INF/config/applicationContextxml
/WEB-INF/config/codeifActionxml
applicationContext.xml
xmlns:xsi="http://wwwworg/2001/XMLSchema-instance" xmlns:p="http://wwwspringframeworkorg/schema/p" xmlns:context="http://wwwspringframeworkorg/schema/context" xsi:schemaLocation="http://wwwspringframeworkorg/schema/beans http://wwwspringframeworkorg/schema/beans/spring-beans-xsd http://wwwspringframeworkorg/schema/context http://wwwspringframeworkorg/schema/context/spring-context-xsd" default-lazy-init="true">
xmlns:xsi="http://wwwworg/2001/XMLSchema-instance" xmlns:p="http://wwwspringframeworkorg/schema/p"
xmlns:context="http://wwwspringframeworkorg/schema/context"
xsi:schemaLocation="http://wwwspringframeworkorg/schema/beans http://wwwspringframeworkorg/schema/beans/spring-beans-xsd
http://wwwspringframeworkorg/schema/context http://wwwspringframeworkorg/schema/context/spring-context-xsd"
default-lazy-init="true">
codeifAction.xml
<beans xmlns="http://wwwspringframeworkorg/schema/beans"
xmlns:xsi="http://wwwworg/2001/XMLSchema-instance" xmlns:context="http://wwwspringframeworkorg/schema/context"
xsi:schemaLocation="http://wwwspringframeworkorg/schema/beans http://wwwspringframeworkorg/schema/beans/spring-beans-xsd
http://wwwspringframeworkorg/schema/context http://wwwspringframeworkorg/schema/context/spring-context-xsd"
default-lazy-init="true">
UploadAction.java
package comcodeifaction;
import javaioFile;
import javautilDate;
import javaxservlethttpHttpServletRequest;
import orgspringframeworkstereotypeController;
import orgspringframeworkuiModelMap;
import orgspringframeworkwebbindannotationRequestMapping;
import orgspringframeworkwebbindannotationRequestParam;
import orgspringframeworkwebmultipartMultipartFile;
@Controller
public class UploadAction {
@RequestMapping(value = "/uploaddo")
public String upload(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request, ModelMap model) {
Systemoutprintln("开始");
String path = requestgetSession()getServletContext()getRealPath("upload");
String fileName = filegetOriginalFilename();
// String fileName = new Date()getTime()+"jpg";
Systemoutprintln(path);
File targetFile = new File(path, fileName);
if(!targetFileexists()){
targetFilemkdirs();
}
//保存
try {
filetransferTo(targetFile);
} catch (Exception e) {
eprintStackTrace();
}
modeladdAttribute("fileUrl", requestgetContextPath()+"/upload/"+fileName);
return "result";
}
}
index.jsp
<%@ page pageEncoding="utf-8"%>
WEB-INF/jsp/下的result.jsp
<%@ page pageEncoding="utf-8"%>
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~