java基于框架上传下载_在SpringMVC框架下实现文件的 上传和 下载

在eclipse中的javaEE环境下:导入必要的架包

web.xml的配置文件:

id="WebApp_ID" version="2.5">

springDispatcherServlet

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:springmvc.xml

1

springDispatcherServlet

/

HiddenHttpMethodFilter

org.springframework.web.filter.HiddenHttpMethodFilter

HiddenHttpMethodFilter

/*

spring的bean的配置文件springmvc.xml;

class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

handler类方法:实现文件的上传和下载的方法

@Controllerpublic classSpringMVCTest {

@AutowiredprivateEmployeeDao employeeDao;//实现文件的下载//需要说明的是文件的上传和下载不需要其他配置

@RequestMapping("testResponseEntity")public ResponseEntity testResponseEntity(HttpSession session) throwsIOException{byte[] body=null;

ServletContext servletContext=session.getServletContext();///files/abc.txt:所要下载文件的地址

InputStream in=servletContext.getResourceAsStream("/files/abc.txt");

body=new byte[in.available()];

in.read(body);

HttpHeaders headers=newHttpHeaders();//响应头的名字和响应头的值

headers.add("Content-Disposition", "attachment;filename=abc.txt");

HttpStatus statusCode=HttpStatus.OK;

ResponseEntity response=new ResponseEntity(body, headers, statusCode);returnresponse;

}

//文件上传,

@RequestMapping("/testFileUpload")

public String testFileUpload(@RequestParam("desc") String desc,

@RequestParam("file") MultipartFile file) throws IOException{

System.out.println("desc:"+desc);

System.out.println("OriginalFilename"+file.getOriginalFilename());

System.out.println("InputStream"+file.getInputStream());

return "success";

}

}

jsp页面:index.jsp:

Insert title here

File:

Desc:

Test ResponseEntity

success.jsp页面:显示文件上传成功

Insert title here

Success page

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值