日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Java實現(xiàn)文件上傳的方法有哪些

這篇文章給大家分享的是有關(guān)Java實現(xiàn)文件上傳的方法有哪些的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

10余年的麒麟網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。營銷型網(wǎng)站建設(shè)的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整麒麟建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)公司從事“麒麟網(wǎng)站設(shè)計”,“麒麟網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。

一. uploadify:

uploadify控件的scripts和styles在這里:圖片上傳

JSP:

<%@ page contentType="text/html;charset=UTF-8" language="java" %> 
<%@ include file="../jsp/include/taglibs.jsp"%> 
 
 
 
  
 Upload 
  
  
  
  
  
  
  
 " rel="external nofollow" > 
  " rel="stylesheet" type="text/css" /> 
  
 "> 
  
  
  #jidAttachTable {width:400px;font-size:14px;border:1px solid #d3d3d3;border-left-width:0;border-top-width:0;} 
  #jidAttachTable td{border: 1px solid #d3d3d3;text-align:left;padding:5px;border-right-width:0;border-bottom-width:0;} 
  #jidAttachTable .i-i-title{width:450px;} 
  #jidAttachTable .i-i-content{width:100px;} 
  
 
  
  
 
 
  
  
   
    
  
                  
   
                
                              ${attach.name}                 刪除                               
   
      
     

后臺Java:

package com.sun.fileUpload; 
 
import java.io.BufferedOutputStream; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.Date; 
import java.util.Iterator; 
import java.util.LinkedList; 
import java.util.List; 
 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
import org.apache.commons.fileupload.FileItem; 
import org.apache.commons.fileupload.FileUploadException; 
import org.apache.commons.fileupload.disk.DiskFileItemFactory; 
import org.apache.commons.fileupload.servlet.ServletFileUpload; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.multipart.MultipartFile; 
 
@Controller 
public class upload{ 
 
 // 附件上傳 
 @RequestMapping("/uploadAttach.do") 
 public void attachUpload(ModelMap modelMap,HttpServletRequest request ,HttpServletResponse response) throws IOException { 
 
  System.out.println("upload file .... start"); 
 
  String savePath = request.getSession().getServletContext().getRealPath("/") + "upload"; 
 
  System.out.println("store path is :" + savePath); 
 
  File f1 = new File(savePath); 
 
  if (!f1.exists()) { 
   f1.mkdirs(); 
  } 
 
  DiskFileItemFactory fac = new DiskFileItemFactory(); 
  ServletFileUpload upload = new ServletFileUpload(fac); 
  upload.setHeaderEncoding("utf-8"); 
 
  List fileList = null; 
  try { 
   fileList = upload.parseRequest(request); 
  } catch (FileUploadException ex) { 
   ex.printStackTrace(); 
   return; 
  } 
 
  Iterator it = fileList.iterator(); 
  String name = ""; 
  System.out.println("deal the files ... start"); 
  //存儲完畢保存進入數(shù)據(jù)庫 
  //Document document = null; 
  while (it.hasNext()) { 
 
   FileItem item = it.next(); 
   if (!item.isFormField()) { 
 
    name = item.getName(); 
    long size = item.getSize(); 
    String type = item.getContentType(); 
 
    if (name == null || name.trim().equals("")) { 
     continue; 
    } 
 
    System.out.println("dealing file info:" + "fileName" + name + " size" + size + " type:" + type); 
 
    //擴展名格式: 
    if (name.lastIndexOf(".") >= 0) { 
     name.substring(name.lastIndexOf(".")); 
    } 
 
    //存文件到磁盤指定路徑 且存儲文件記錄存入數(shù)據(jù)庫,如果存儲發(fā)生故障,數(shù)據(jù)庫記錄也會創(chuàng)建失敗 
/*    document = new Document(); 
    document.setFileType(extName); 
    document.setFullName(name); 
    document.setDescription(name); 
    document.setName(name); 
    document.setUploadDate(new Date()); 
    document = documentManager.saveWithFileStore(document,item,savePath,extName);*/ 
    System.out.println(new Date() + "persist id :" + name); 
    File saveFile = new File(savePath + "/" + name); 
    try { 
     item.write(saveFile); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     throw new RuntimeException(new Date() + "store file error"); 
    } 
   } 
  } 
 
  System.out.println("deal the files end"); 
  System.out.println("upload file .... end"); 
 
  response.getWriter().print(name + "@" + name); 
 } 
}

這里把數(shù)據(jù)庫更新給省略了,有需要可以自己添加。

除上傳外,其他實現(xiàn)功能:
1. 視頻上傳后的預(yù)覽功能
2. 視頻是單文件上傳,附件是多文件上傳和刪除

二. Spring默認的上傳功能:
JSP:

<%@ page contentType="text/html;charset=UTF-8" language="java" %> 
<%@ include file="../jsp/include/taglibs.jsp"%> 
 
 
 
  
 Upload 
  
  
  
 " rel="external nofollow" > 
  
  
  .file { 
   position: relative; 
   display: inline-block; 
   background: #428BCA; 
   border: 1px solid #99D3F5; 
   border-radius: 4px; 
   padding: 4px 12px; 
   overflow: hidden; 
   color: #000000; 
   text-decoration: none; 
   text-indent: 0; 
   line-height: 20px; 
  } 
  .file:link { 
   color: white; 
   text-decoration:none; 
  } 
  .file input { 
   position: absolute; 
   font-size: 100px; 
   right: 0; 
   top: 0; 
   opacity: 0; 
  } 
  .file:hover { 
   background: #1B6AAA; 
   border-color: #78C3F3; 
   color: white; 
   text-decoration: none; 
  } 
  
 
  
 
 
  
  點擊保存后上傳: 
  選擇文件 
      
   
   
   
  
 

后臺Java:

package com.sun.fileUpload; 
 
import java.io.BufferedOutputStream; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.Date; 
import java.util.Iterator; 
import java.util.LinkedList; 
import java.util.List; 
 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
import org.apache.commons.fileupload.FileItem; 
import org.apache.commons.fileupload.FileUploadException; 
import org.apache.commons.fileupload.disk.DiskFileItemFactory; 
import org.apache.commons.fileupload.servlet.ServletFileUpload; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.multipart.MultipartFile; 
 
@Controller 
public class upload{ 
 @RequestMapping(value = "/uploadFile.do") 
 public String uploadController(HttpServletRequest request,ModelMap modelMap, 
   @RequestParam("videoFile") MultipartFile videoFile) throws IllegalStateException, IOException{ 
  String savePath = request.getSession().getServletContext().getRealPath("/") + "upload"; 
   
  try { 
   uploadSingleFile(savePath, videoFile, request.getSession().getServletContext().getRealPath("/")); 
  }catch (Exception e) { 
   return "/upload"; 
  } 
 
  modelMap.addAttribute("uploadfile", "upload/" + videoFile.getOriginalFilename()); 
   
  return "/upload"; 
 }  
 
 /** 
  * 
  * @param path 這個path 是upload的子目錄路徑 比如 path=/image 最終將下載到[root/upload/image/]目錄下 
  * @param file 
  * @return 
  * @throws java.io.IOException 
  */ 
 public static String uploadSingleFile(String path, MultipartFile file, String rootPath) { 
   
  if (!file.isEmpty()) { 
    
    byte[] bytes; 
    try { 
     bytes = file.getBytes(); 
     
     // Create the file on server 
     File serverFile = new File(path + "/" + file.getOriginalFilename()); 
     BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile)); 
     stream.write(bytes); 
     stream.close(); 
  
     System.out.println("Server File Location=" + serverFile.getAbsolutePath()); 
 
     return getRelativePathFromUploadDir(serverFile, rootPath).replaceAll("\\\\", "/"); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    
  }else{ 
   System.out.println("文件內(nèi)容為空"); 
  } 
  return null;  
 } 
  
 /** 
  * 
  * @param file 
  * @return 返回從upload目錄下面的相對路徑 
  */ 
 public static String getRelativePathFromUploadDir(File file, String rootPath){ 
  if(null==file) 
   return ""; 
  String absolutePath = file.getAbsolutePath(); 
  if(absolutePath.indexOf(rootPath)!=-1 && rootPath.length()

Spring配置文件springmvc-servlet.xml中添加:

 
 

如果和uploadify同時使用的話,需要做一些修改,否則uploadify的上傳文件會被Spring攔截:

  
lt;bean id="multipartResolver" class="org.sun.com.MyMultipartResolver"> 
  
  
  UTF-8 
  
 
lt;/bean>

org.sun.com.MyMultipartResolver: 

package org.sun.com; 
 
import javax.servlet.http.HttpServletRequest; 
 
import org.springframework.web.multipart.commons.CommonsMultipartResolver; 
 
/** 
 * @author Sun 
 */ 
public class MyMultipartResolver extends CommonsMultipartResolver { 
 private String excludeUrls;  
 private String[] excludeUrlArray; 
  
 public String getExcludeUrls() { 
  return excludeUrls; 
 } 
 
 public void setExcludeUrls(String excludeUrls) { 
  this.excludeUrls = excludeUrls; 
  this.excludeUrlArray = excludeUrls.split(","); 
 } 
 
 /** 
  * 這里是處理Multipart http的方法。如果這個返回值為true,那么Multipart http body就會MyMultipartResolver 消耗掉.如果這里返回false 
  * 那么就會交給后面的自己寫的處理函數(shù)處理例如剛才ServletFileUpload 所在的函數(shù) 
  * @see org.springframework.web.multipart.commons.CommonsMultipartResolver#isMultipart(javax.servlet.http.HttpServletRequest) 
  */ 
 @Override 
 public boolean isMultipart(HttpServletRequest request) { 
  for (String url: excludeUrlArray) { 
   // 這里可以自己換判斷 
   if (request.getRequestURI().contains(url)) { 
    return false; 
   } 
  } 
   
  return super.isMultipart(request); 
 } 
  
}

感謝各位的閱讀!關(guān)于“Java實現(xiàn)文件上傳的方法有哪些”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!


網(wǎng)站名稱:Java實現(xiàn)文件上傳的方法有哪些
本文路徑:http://www.dlmjj.cn/article/ipeocd.html