springboot for file upload and download

1、jsp

Upload function:

<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 =" upload "the onclick =" the uploadFile (); "/> download:</ TR>
</ TD>


<a href="<%=path %>/file/download?fileName=${bean.attach_file }">${bean.attach_file }</a>


2, JAVA background

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);

/**
* Single file upload
* @param a MultipartHttpServletRequest
* @return
* /
@ RequestMapping (value = "/ the Upload")
@ResponseBody
public String the Upload (the HttpServletRequest Request, a MultipartHttpServletRequest a MultipartHttpServletRequest) {
// Get upload file path
MultipartFile uploadFile = multipartHttpServletRequest.getFile ( "File");
String uploadFilePath = "";
IF (the uploadFile = null) {!
uploadFilePath uploadFile.getOriginalFilename = ();
System.out.println ( "uploadFlePath:" + uploadFilePath);
} the else {
return "2";
}
// interception upload file name
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 ()) {// file is determined whether there is a parent directory
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();
}
}
}
}

Guess you like

Origin www.cnblogs.com/wzb0228/p/11343669.html