package cn.bdqn.controller;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.log4j.Logger;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
/**
* @author: 赵林
* @Date: 2019/9/6 8:49
* @Description:
*/
@Controller
@RequestMapping("/f")
public class fileManagerController {
private Logger logger=Logger.getLogger(fileManagerController.class);
@RequestMapping(value = "/upload.do",method = RequestMethod.GET)
public String toFile(){
return "fileUpload";
}
/**
*单文件上传
* @return
*/
@RequestMapping(value = "/one.html",method = RequestMethod.POST)
public String fileupload(HttpServletRequest request, @RequestParam(value = "file",required = false) MultipartFile file) throws IOException {
//判断是否有上传的文件
if (!file.isEmpty()) {
//输出文件的名字
logger.debug("文件的名称:==============="+file.getOriginalFilename());
logger.debug("文件的大小:==============="+file.getSize()/1024+"k");
logger.debug("文件的后缀:==============="+FilenameUtils.getExtension(file.getOriginalFilename()));
//设置文件上传的目录(完整名+文件名)
String uploadPath=request.getServletContext().getRealPath("/downloads/")+File.separator +System.currentTimeMillis()+file.getOriginalFilename();
logger.debug("文件上传的目录================>"+uploadPath);
//目标文件
File fileptah=new File(uploadPath);
//上传文件
FileUtils.copyInputStreamToFile(file.getInputStream(),fileptah);
}
return "上传成功!";
}
/**
*单文件上传
* @return
*/
@RequestMapping(value = "/two.html",method = RequestMethod.POST)
public String fileupload2(HttpServletRequest request, @RequestParam MultipartFile[] files) throws IOException {
//判断是否有上传的文件
if (files.length!=0) {
for (MultipartFile file : files) {
//输出文件的名字
logger.debug("文件的名称:==============="+file.getOriginalFilename());
logger.debug("文件的大小:==============="+file.getSize()/1024+"k");
logger.debug("文件的后缀:==============="+FilenameUtils.getExtension(file.getOriginalFilename()));
//设置文件上传的目录(完整名+文件名)
String uploadPath=request.getServletContext().getRealPath("/downloads/")+File.separator +System.currentTimeMillis()+file.getOriginalFilename();
logger.debug("文件上传的目录================>"+uploadPath);
//目标文件
File fileptah=new File(uploadPath);
//上传文件
FileUtils.copyInputStreamToFile(file.getInputStream(),fileptah);
}
}
return "上传成功!";
}
@RequestMapping(value = "/down.html")
public String toDown(){
return "download";
}
@RequestMapping(value = "/download.html")
public ResponseEntity<byte[]> download(@RequestParam String fileName,HttpServletRequest request) throws IOException {
//获取文件在服务器上的目录
String realPath = request.getServletContext().getRealPath("/WEB-INF/jsp/img/");
//最终生成全路径名
File file=new File(realPath+File.separator+fileName);
//定义请求头
HttpHeaders httpHeaders=new HttpHeaders();
//下载完成后显示文件名(中文乱码)
String downloadFileName=new String(
fileName.getBytes("UTF-8"),
"ISO-8859-1");
//通知浏览器以附件形式下载文件
httpHeaders.setContentDispositionFormData("attachment",downloadFileName);
//定义以二进制流形式下载返回文件数据
httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//下载:返回ResponseEntity对象
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),httpHeaders,HttpStatus.CREATED);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
- ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。
资源推荐
资源详情
资源评论
收起资源包目录
基于ssm产品项目.zip (61个子文件)
code
resources
springMVC-servlet.xml 5KB
mybatis-config.xml 311B
log4j.properties 3KB
database.properties 160B
applicationContext-mybatis.xml 3KB
src
cn
bdqn
controller
json.java 1KB
provider.java 4KB
fileManagerController.java 5KB
dao
UserMapper.java 473B
UserMapper.xml 2KB
pojo
User.java 3KB
service
ProviderServiceimpl.java 1KB
ProviderService.java 424B
Test.java 700B
web
WEB-INF
lib
commons-io-2.4.jar 181KB
spring-tx-3.2.13.RELEASE.jar 237KB
druid-1.1.19.jar 2.65MB
jstl.jar 20KB
spring-jdbc-3.2.13.RELEASE.jar 396KB
spring-aop-3.2.13.RELEASE.jar 331KB
commons-fileupload-1.2.2.jar 58KB
spring-expression-3.2.13.RELEASE.jar 192KB
spring-webmvc-3.2.13.RELEASE.jar 626KB
mybatis-3.5.1.jar 1.61MB
spring-beans-3.2.13.RELEASE.jar 600KB
fastjson-1.2.13.jar 384KB
log4j-1.2.17.jar 478KB
standard.jar 384KB
spring-web-3.2.13.RELEASE.jar 617KB
aopalliance-1.0.jar 4KB
spring-core-3.2.13.RELEASE.jar 865KB
mybatis-spring-1.3.2.jar 52KB
commons-logging-1.1.1.jar 59KB
mysql-connector-java-5.1.0-bin.jar 553KB
freemarker-2.3.23.jar 1.29MB
spring-context-3.2.13.RELEASE.jar 848KB
spring-context-support-3.2.13.RELEASE.jar 126KB
aspectjweaver-1.6.9.jar 1.59MB
web.xml 2KB
jsp
indexs.jsp 442B
fileUpload.jsp 1KB
download.jsp 398B
js
jquery-1.8.3.min.js 91KB
img
2.jpg 30KB
过度.jpg 42KB
1.jpg 61KB
0.jpg 68KB
Add.jsp 2KB
update.jsp 3KB
getCha.jsp 7KB
index.jsp 175B
.idea
project-template.xml 225B
description.html 36B
artifacts
web_war_exploded.xml 493B
scopes
scope_settings.xml 139B
libraries
commons_fileupload_1_2_2.xml 250B
lib.xml 281B
misc.xml 6KB
modules.xml 264B
copyright
profiles_settings.xml 111B
Mvc_provider.iml 1KB
共 61 条
- 1
资源评论
奋斗奋斗再奋斗的ajie
- 粉丝: 229
- 资源: 2104
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功