博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springmvc学习笔记(九)文件上传
阅读量:3530 次
发布时间:2019-05-20

本文共 2054 字,大约阅读时间需要 6 分钟。

方式一:

1.spring配置文件

2.上传页面jsp

选择文件:
3.Controller类

package com.skymr.mvc.controller.annotation;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.multipart.commons.CommonsMultipartFile;@Controller@RequestMapping("file")public class UploadController {	@RequestMapping("/upload")	public String upload(@RequestParam("file")CommonsMultipartFile file){		String fileName = file.getOriginalFilename();		System.out.println("收到上传文件" + fileName);		if(file.isEmpty()){			System.out.println("上传失败");		}		try {			InputStream is = file.getInputStream();			FileOutputStream fos = new FileOutputStream("E:/"+fileName);			int b;			while((b=is.read()) != -1){				fos.write(b);			}			fos.flush();			fos.close();			is.close();		} catch (IOException e) {		}		return "uploadSuc";	}	@RequestMapping("/uploadView")	public String uploadView(){		return "uploadView";	}}
部署测试后能上传成功,但上传较大文件时抛出异常,却不知道在哪捕获.

方式二:使用springmvc扩展的HttpServletReqeust,MultipartHttpServletRequest

@RequestMapping("/upload2")	public String upload2(HttpServletRequest request, HttpServletResponse response){		//解析器		CommonsMultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());		if(resolver.isMultipart(request)){			MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;			Iterator
iter = multiRequest.getFileNames(); while(iter.hasNext()){ String name = iter.next(); MultipartFile file = multiRequest.getFile(name); try { System.out.println("收到上传文件" + file.getOriginalFilename()); file.transferTo(new File("E:/"+file.getOriginalFilename())); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } return "uploadSuc"; }

转载地址:http://mjihj.baihongyu.com/

你可能感兴趣的文章
【STM32+W5500+HTTPClient】25,路由器DHCP租赁IP时间为2h,NetBios可以很好的解决IP变化的问题,DNS,2018年12月25日
查看>>
【STM32+MQTT+ONENET】26,MQTT协议接入OneNET
查看>>
【STM32+W5500+MQTT+ONENET】27,MQTT协议接入OneNET实际编程操作 2018年12月27日
查看>>
【STM32Cube+FreeRTOS 】28,KEIL5的F12不起作用;***JLink Error: Can not read register x while CPU is running
查看>>
【STM32CubeMX+FreeRTOS 】29,prtinf卡死;4任务只运行了3个;W5500联网失败(堆栈不能太大或者太小)
查看>>
【STM32+FreeRTOS +W5500移植要点】30,RTOS中断;从TIM2,主TIM3;RTOS主要用在LCD中;RT-Thread;标志重定义问题 2019年01月22日
查看>>
【STM32+FPGA+FSMC】31,FSMC熟练掌握;KEIL5生成bin文件;SDRAM的使用;IAP检验码 2019年04月10日
查看>>
【IC1】【转 非常好】运算放大器使用的六个经验
查看>>
【IC-ADC 3】ADC的选型
查看>>
2019年03月18日 查看数据手册的注意点,极限参数、电气参数、推荐参数
查看>>
HiKey960/970用户手册;HiKey960 Development Board User Manual
查看>>
【书籍推荐】FPGA,xilinx
查看>>
N9-SQL注入(union注入)
查看>>
N10-sql注入(information_schema注入)
查看>>
N1-Kali虚拟机中SQLmap
查看>>
N11-sql注入(http头注入)
查看>>
N2-sqlmap初使用
查看>>
N12-sql盲注原理以及boolean盲注案例实现
查看>>
N13-sqli盲注 基于时间型
查看>>
N1 技术心得 2019-6-26
查看>>