tomcat一启动,便为所有的后端控制器生成对象,
- 也可以在Spring的配置文件中添加
- Xml代码
- <bean id=”multipartResolver” class=”org.springframework.web.multipart.commons.CommonsMultipartResolver”> //定义上传处理器
- <property name=”defaultEncoding” value=”UTF-8″ />
- <property name=”maxInMemorySize" value="10240"/>
- <property name=” uploadTempDir value=”/upload/"/>
- <property name=” maxUploadSize value=”-1"/>
- </bean>
@RequestParam("myFile") CommonsMultipart File file//上传文件
String path=req.getSession().getServletContext().getRealPath("/upload/");
file.getOriginalFilename()//拿到原始文件名
filename.subString(fileName.lastIndexOf(".")+1)//截取文件扩展名
File file1=new File(path,file);//新建一个文件对象
Byte[] b=file.getBytes();拿到字节数组对象
FileOutputStream fos=new FileOutputStream(file1);创建文件输出流对象
fos.write(b);//把字节数组输出到要文件中