java文件下载controller_Java Spring MVC 上传下载文件配置及controller方法详解

Java Spring MVC 上传下载文件配置及controller方法详解

发布于 2020-7-30|

复制链接

摘记: 下载:1.在spring-mvc中配置(用于100M以下的文件下载)

```java

..

下载:1.在spring-mvc中配置(用于100M以下的文件下载)

```java

```

下载文件代码

```java

@RequestMapping("/file/{name.rp}")

public ResponseEntity fileDownLoad(@PathVariable("name.rp")String name, HttpServletRequest request,HttpServletResponse response) {

// @PathVariable String name,

// @RequestParam("name")String name,

// System.out.println(""+name);

// System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

ResponseEntity re = null;

try {

/**

* css,js,json,gif,png,bmp,jpg,ico,doc,docx,xls,xlsx,txt,swf,pdf

* **/

//下载防止静态加载干扰

Feelutile f=new Feelutile();

name=f.getfileformat(name);

String pathString="C:\\tempDirectory\\"+name;

File file=new File(pathString);

HttpHeaders headers=new HttpHeaders();

//String filename=URLEncoder.encode(name, "UTF-8");//为了解决中文名称乱码问题

String filename=new String(name.getBytes("utf-8"),"utf-8");

byte[] by=FileUtils.readFileToByteArray(file);

headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);

//URLEncoder.encode(filename, "UTF-8")

headers.setContentDispositionFormData("attachment",filename);

headers.setContentLength(by.length);

re=new ResponseEntity(by, headers, HttpStatus.CREATED);

} catch (Exception e) {

e.printStackTrace();

try {

request.getRequestDispatcher("/error/404.jsp").forward(request, response);

} catch (ServletException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

} catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

return re;

}

```

上传文件:1在spring-mvc中配置

```java

UTF-8

1048576000

40960

```

在controller中代码如下

```java

@RequestMapping(value="/upload", method = RequestMethod.POST)

@ResponseBody

public Json upload(Doc doc, @RequestParam("uploadFile") CommonsMultipartFile file) {

Json j = new Json();

try {

String realpath = this.servletContext.getRealPath("/upload");

String uploadFileFileName = file.getOriginalFilename();

String uploadFileFileNameWithoutSpace = uploadFileFileName.replaceAll(" ", "");

String fileType = uploadFileFileNameWithoutSpace.substring(uploadFileFileNameWithoutSpace.lastIndexOf("."));

File targetFile = new File(realpath+File.separator, uploadFileFileNameWithoutSpace);

if (targetFile.exists()) {

targetFile.delete();

}

file.getFileItem().write(targetFile);

docService.upload(doc,uploadFileFileNameWithoutSpace);

j.setSuccess(true);

j.setMsg("Upload manual successfully");

}catch (Exception e) {

logger.error(ExceptionUtil.getExceptionMessage(e));

j.setMsg("Upload manual unsuccessfully");

}

return j;

}

```

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值