java web文件上传与下载

jsp代码(文件上传)
   
   

   
   
  1. <form id="upLoad" method="post">
  2. <input type="file" id="yaFile" name="file"> <br/>
  3. <input id="fileSubmit" type="button" value="提交">
  4. </form>
js代码:这里我选择的是异步提交
   
   

   
   
  1. $("#fileSubmit").click(function(){
  2. var formData = new FormData();
  3. formData.append("file", document.getElementById("upLoad").files[0]);
  4. $.ajax({
  5. cache: true,
  6. type: "POST",
  7. url:'路径',
  8. data:formData,
  9. processData: false, //禁止jquery对DAta数据的处理,默认会处理
  10. contentType: false, //禁止设置请求类型
  11. async: false,
  12. error: function(request) {
  13. alert("上传失败");
  14. },
  15. success: function(data) {
  16. if(data==0){
                        alert("上传成功");
   
   
} } });});
 
  
java代码:文件上传
   
   

   
   
  1. /**
  2. * 文件上传功能
  3. * @param file
  4. * @return
  5. * @throws IOException
  6. */
  7. @RequestMapping( "/upload")
  8. @ResponseBody
  9. public String upload(MultipartFile file,String faUrl,HttpServletRequest request) throws IOException{
  10. String path = request.getSession().getServletContext().getRealPath( "upload"); //文件上传路径
  11. String fileName = file.getOriginalFilename();
  12. File dir = new File(path ,fileName);
  13. if(!dir.exists()){
  14. dir.mkdirs();
  15. }
  16. //MultipartFile自带的解析方法
  17. file.transferTo(dir);
  18. return "0";
  19. }
js代码:文件下载
   
   

   
   
  1. function onClick() {
  2. var url ="路径"
  3. document.location.href=url;
  4. }
java代码:文件下载
   
   

   
   
  1. /**
  2. * 文件下载功能
  3. * @param request
  4. * @param response
  5. * @throws Exception
  6. */
  7. @RequestMapping( "/fileDown")
  8. @ResponseBody
  9. public void fileDown(HttpServletRequest request,HttpServletResponse response){
  10. //通过文件名找出文件的所在目录
  11. String path = request.getSession().getServletContext().getRealPath( "upload");
  12. ServletOutputStream out;
  13. //得到要下载的文件
  14. File file = new File(path);
  15. try {
  16. //设置响应头,控制浏览器下载该文件
  17. response.setContentType( "multipart/form-data");
  18. //获得浏览器信息,并处理文件名
  19. String headerType=request.getHeader( "User-Agent").toUpperCase();
  20. String fileName = null;
  21. if (headerType.indexOf( "EDGE") > 0||headerType.indexOf( "MSIE")> 0||headerType.indexOf( "GECKO")> 0) {
  22. fileName=URLEncoder.encode(file.getName(), "UTF-8");
  23. } else{
  24. fileName= new String(file.getName().replaceAll( " ", "").getBytes( "utf-8"), "iso8859-1");
  25. }
  26. response.addHeader( "Content-Disposition", "attachment;filename="+fileName);
  27. response.addHeader( "Content-Length", "" + file.length());
  28. FileInputStream inputStream = new FileInputStream(file);
  29. out = response.getOutputStream();
  30. int b = 0;
  31. byte[] buffer = new byte[ 1024];
  32. while (b != - 1) {
  33. b = inputStream.read(buffer);
  34. //写到输出流(out)中
  35. if(b!=- 1)
  36. out.write(buffer, 0, b);
  37. }
  38. inputStream.close();
  39. out.close(); //关闭输出流
  40. out.flush();
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. }
  44. }
以上为文件上传、文件下载的全部代码。









猜你喜欢

转载自blog.csdn.net/qq_42239765/article/details/82773577