springboot实现文件上传与下载

1、jsp

上传功能:

<tr class="main_table_tr">
<td class="td_title"><p class="add_table_p2">附件:</p></td>
<td>
<input type="text" id="attach_file" name="attach_file" class="td_input" value="${bean.attach_file}"
placeholder="点击选择文件" onclick="javascript:$('input[name=\'inputFile\']').click();" readonly style="width: 844px;"/>
<input type="file" id="inputFile" name="inputFile" style="display:none;"
onchange="javascript:$('input[name=\'attach_file\']').val(this.files[0].name);"/>
<input type="button" value="上传" onclick="uploadFile();" />
</td>
</tr>

下载功能:
<a href="<%=path %>/file/download?fileName=${bean.attach_file }">${bean.attach_file }</a>


2、JAVA后台

package com.csg.scarmknowledge.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

/**
* 文件上传下载
*/
@Controller
@RequestMapping("/file")
public class FileController {

private Logger log = LoggerFactory.getLogger(FileController.class);

/**
* 单文件上传
* @param multipartHttpServletRequest
* @return
*/
@RequestMapping(value = "/upload")
@ResponseBody
public String upload(HttpServletRequest request, MultipartHttpServletRequest multipartHttpServletRequest) {
// 获取上传文件的路径
MultipartFile uploadFile = multipartHttpServletRequest.getFile("file");
String uploadFilePath = "";
if (uploadFile != null) {
uploadFilePath = uploadFile.getOriginalFilename();
System.out.println("uploadFlePath:" + uploadFilePath);
} else {
return "2";
}
// 截取上传文件的文件名
String uploadFileName = uploadFilePath.substring(uploadFilePath.lastIndexOf('\\') + 1, uploadFilePath.indexOf('.'));
System.out.println("multiReq.getFile()" + uploadFileName);
// 截取上传文件的后缀
String uploadFileSuffix = uploadFilePath.substring(uploadFilePath.indexOf('.') + 1, uploadFilePath.length());
System.out.println("uploadFileSuffix:" + uploadFileSuffix);
FileOutputStream fos = null;
FileInputStream fis = null;
try {
fis = (FileInputStream) multipartHttpServletRequest.getFile("file").getInputStream();
fos = new FileOutputStream(new File("E:\\uploadFiles\\" + uploadFileName + ".") + uploadFileSuffix);
byte[] temp = new byte[1024];
int i = fis.read(temp);
while (i != -1){
fos.write(temp,0,temp.length);
fos.flush();
i = fis.read(temp);
}
return "1";
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return "0";
}

/**
* 文件下载
* @param fileName
* @param response
* @return
*/
@GetMapping("/download")
@ResponseBody
public void downloadFile(@RequestParam("fileName") String fileName,HttpServletResponse response) throws UnsupportedEncodingException {
String filePath = "E:\\uploadFiles" ;
File file = new File(filePath + "/" + fileName);
if(file.exists()){ //判断文件父目录是否存在
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
// response.setContentType("application/force-download");
response.setHeader("Content-Disposition", "attachment;fileName=" + java.net.URLEncoder.encode(fileName,"UTF-8"));
byte[] buffer = new byte[1024];
FileInputStream fis = null; //文件输入流
BufferedInputStream bis = null;

OutputStream os = null; //输出流
try {
os = response.getOutputStream();
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
int i = bis.read(buffer);
while(i != -1){
os.write(buffer);
i = bis.read(buffer);
}

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("----------file download---" + fileName);
try {
bis.close();
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

猜你喜欢

转载自www.cnblogs.com/wzb0228/p/11343669.html