javaweb upload and download the full version of of parse
- 2020-06-03 06:20:54
- OfStack
1. Display downloaded file resources
To provide users with file resources in Web application system for downloading, we first need a page to list all the files in the uploaded file directory. When users click the file download hyperlink, they will download and write an ListFileServlet to list all the downloaded files in Web application system.
1.1 File download page
download. html code is as follows:
<!DOCTYPE HTML>
<html>
<head>
<title> Download the file display page </title>
</head>
<body>
<div id="fileName"></div>
</body>
<script >
$(function(){
download();
});
function download(){
$.ajax({
url: 'cloud/load/download',
type: 'POST',
dataType:'JSON',
cache: false,
processData: false,
contentType: false,
success : function(date){
var file="";
$.each(date,function(key,values){
var newKey = "/D:/Download/"+key;
file += "<div>"+key+" "+"<a href='cloud/load/downloadFile?fileName="+key+"'>"+" download "+"</a>"+"</div>"+"<br>";
$(values).each(function(){
file+="\t"+this;
});
});
alert("success");
},
error : function(e){
alert("error");
}
});
}
</script>
</html>
1.2 controller
@RequestMapping(value = "/download", method = RequestMethod.POST)
@ResponseBody
public Map<String,String> download(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws ServletException, IOException{
Map<String,String> map = fileLoadService.doGet(request, response);
return map;
}
1.3 service
/**
* File download display
* @ClassName: FileLoadServiceImpl
* @throws IOException
* @throws ServletException
*/
@Override
public Map<String,String> doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
// Gets the directory of the uploaded files
String uploadFilePath = "/D:/Download/";
// Store the file name to download
Map<String,String> fileNameMap = new HashMap<String,String>();
// The recursive traversal filepath All files and directories under the directory, store the file name of the file to map In the collection
listfile(new File(uploadFilePath),fileNameMap);
return fileNameMap;
}
public void listfile(File file,Map<String,String> map){
// if file Does not represent 1 A file, but 1 A directory
if(!file.isFile()){
// Lists all the files and directories in this directory
File files[] = file.listFiles();
// traverse files[] An array of
for(File f : files){
// recursive
listfile(f,map);
}
}else{
String realName = file.getName().substring(file.getName().indexOf("_")+1);
//file.getName() You get the original name of the file, which is unique 1 So it can be used as key . realName Is a processed name that may be repeated
map.put(file.getName(), realName);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
2. Download the displayed file resources
2.1 controller
@RequestMapping(value = "/downloadFile", method = RequestMethod.GET)
@ResponseBody
public void downloadFile(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws ServletException, IOException{
String filename =request.getParameter("fileName");
fileLoadService.doGetFile(request, response ,filename);
}
2.2 service
/**
* Download the file locally start
*/
@Override
public void doGetFile(HttpServletRequest request, HttpServletResponse response,String filename) throws ServletException,IOException {
// Gets the file name to download
String fileName = filename;
fileName = new String(fileName.getBytes("iso8859-1"),"UTF-8");
String fileSaveRootPath="/D:/Download";
File file = new File(fileSaveRootPath + "/" + fileName);
// If the file doesn't exist
if(!file.exists()){
request.setAttribute("message", " The resource you downloaded has been deleted!! ");
return;
}
// Handle filename
String realname = fileName.substring(fileName.indexOf("_")+1);
// Set the response header to control the browser to download the file
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(realname, "UTF-8"));
InputStream fis = new BufferedInputStream(new FileInputStream(fileSaveRootPath + "\\" + fileName));
byte[] buffer = new byte[fis.available()];
fis.read(buffer); // Read file stream
fis.close();
response.reset(); // Reset result set
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(realname, "UTF-8"));
response.addHeader("Content-Length", "" + file.length()); // Returns the first The file size
response.setContentType("application/octet-stream"); // Set the data type
OutputStream os = new BufferedOutputStream(response.getOutputStream());
os.write(buffer); // The output file
os.flush();
os.close();
}
public void doPostFile(HttpServletRequest request, HttpServletResponse response,String filename)throws ServletException, IOException {
doGetFile(request, response,filename);
}
Download the above files.