java實現(xiàn)文件上傳、下載、圖片預覽

這篇文章主要介紹了java實現(xiàn)文件上傳、下載、圖片預覽,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務,包含不限于成都做網(wǎng)站、成都網(wǎng)站制作、孫吳網(wǎng)絡推廣、小程序設計、孫吳網(wǎng)絡營銷、孫吳企業(yè)策劃、孫吳品牌公關、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務,您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)為所有大學生創(chuàng)業(yè)者提供孫吳建站搭建服務,24小時服務熱線:18982081108,官方網(wǎng)址:bm7419.com

多文件保存到本地:

@ResponseBody
  @RequestMapping(value = "/uploadApp",produces = { "application/json;charset=UTF-8" },method= RequestMethod.POST)
  public String uploadApp( HttpServletRequest request,@RequestParam("file") MultipartFile[] file) throws IOException {
    try {
      if(file.length > 0) {
        String name = file[0].getOriginalFilename().split(";")[0];
        String fileUrlName = CommonEnum.FILEPATH+"/"+name;
        for (int i = 0; i < file.length; i++) {
          FileUtils.copyInputStreamToFile(file[i].getInputStream(), new File(fileUrlName, file[i].getOriginalFilename().split(";")[1]));
        }
        return "success";
      }else{
        return "null";
      }
    }catch (Exception e){
      e.printStackTrace();
      return "error";
    }
  }

下載文件:

  @RequestMapping(value = "/download", method = RequestMethod.GET)
  @ResponseBody
  public void download(@RequestParam Map<String, Object> data, HttpServletRequest request,HttpServletResponse response) throws FileNotFoundException {
    String time = DateUtil.formatFromDate("yyyyMMddHHmmss", new Date());
    List<Map<String, Object>> urllist = companyService.findByIMG(data);
    String path = (String) urllist.get(0).get("imgurl");
    String docx = StringUtils.substringAfterLast(path, ".");
    String fileName = time+"."+docx; // 文件的默認保存名
    InputStream inStream = new FileInputStream(path);// 文件的存放路徑
    response.reset();
    response.setContentType("bin");
    response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
    byte[] b = new byte[100];
    int len;
    try {
      while ((len = inStream.read(b)) > 0)
        response.getOutputStream().write(b, 0, len);
      inStream.close();
    } catch (IOException e) {
      e.printStackTrace();
    }

  }

 

  /**
   * 讀取圖片
   */
  @RequestMapping(value = "/iomoreimgcom", produces = {
      "application/json;charset=UTF-8" }, method = RequestMethod.GET)
  @ResponseBody
  public synchronized void iomoreimgcom(HttpServletRequest request, HttpServletResponse response) throws Exception {
    String url = request.getParameter("url");
    File file = new File(url);
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
    BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
    response.setHeader("Content-Type", "image/jpeg");
    byte b[] = new byte[1024];
    int read;
    try {
      while ((read = bis.read(b)) != -1) {
        bos.write(b, 0, read);
      }
      //request.getRequestDispatcher("/components/hazard/yscchird.html").forward(request, response);
    } catch (Exception e) {
      // TODO: handle exception
    } finally {
      if (bos != null) {
        bos.close();
      }
      if (bis != null) {
        bis.close();
      }
    }
  }

前端請求直接拼接圖片路徑即可。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

本文名稱:java實現(xiàn)文件上傳、下載、圖片預覽
轉載注明:http://bm7419.com/article36/jcegpg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供手機網(wǎng)站建設、自適應網(wǎng)站移動網(wǎng)站建設、建站公司定制網(wǎng)站、云服務器

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設