΢ Ͷ ^ ھ

赛季cba联赛赛程 :Jsp+ServletFļςd ļςһ

cba㶫 www.axwwg.com  •rg2019-08-25 10:59:15   ߣ   ҪuՓ(0)

ļςdJava Web؂似܌

ʹõApacheµļςM

org.apache.commons.fileupload F

Y

ļςdJava Web؂似܌
ʹõApacheµļςM
org.apache.commons.fileupload F
YһЩYԼԼćLԇȌһƪļςm𲽌FdչʾļбςϢ־û

UԒfֱϴa

һjarOςĿ

commons-fileupload-1.3.1.jar
commons-io-2.4.jar

ςĿ䛣WEB-INF/tempFilesWEB-INF/uploadFiles

ڶJSP

<%@ page contentType="text/html;charset=UTF-8" language="java" %> 
<%@ taglib prefix="c" uri="//java.sun.com/jsp/jstl/core" %> 
<html> 
<head> 
 <title>ļςyԇ</title> 
</head> 
<body> 
<form method="POST" enctype="multipart/form-data" action="<%=request.getContextPath()%>/UploadServlet"> 
 ļ: <input type="file" name="upfile"><br/> 
 <br/> 
 <input type="submit" value="ς"> 
</form> 
<c:if test="${not empty errorMessage}"> 
 <input type="text" id="errorMessage" value="${errorMessage}" style="color:red;" disabled="disabled"> 
</c:if> 
</body> 
</html> 

Servlet̎ļςĺ

package servlet; 
 
import org.apache.commons.fileupload.FileItem; 
import org.apache.commons.fileupload.FileUploadBase; 
import org.apache.commons.fileupload.FileUploadException; 
import org.apache.commons.fileupload.ProgressListener; 
import org.apache.commons.fileupload.disk.DiskFileItemFactory; 
import org.apache.commons.fileupload.servlet.ServletFileUpload; 
 
import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.Calendar; 
import java.util.Iterator; 
import java.util.List; 
import java.util.UUID; 
 
/** 
 * ̎ļς 
 * 
 * @author xusucheng 
 * @create 2017-12-27 
 **/ 
@WebServlet("/UploadServlet") 
public class UploadServlet extends HttpServlet { 
 @Override 
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
  //Oļς· 
  String savePath = this.getServletContext().getRealPath("/WEB-INF/uploadFiles"); 
  //ORrļ· 
  String tempPath = this.getServletContext().getRealPath("/WEB-INF/tempFiles"); 
  File tempFile = new File(tempPath); 
  if (!tempFile.exists()) { 
   tempFile.mkdir(); 
  } 
 
  //xϢ 
  String errorMessage = ""; 
  //file itemsS 
  DiskFileItemFactory factory = new DiskFileItemFactory(); 
  //Oþ_^С 
  factory.setSizeThreshold(1024 * 100); 
  //ORrļ· 
  factory.setRepository(tempFile); 
  //ļς̎ 
  ServletFileUpload upload = new ServletFileUpload(factory); 
  //O ļςM 
  ProgressListener progressListener = new ProgressListener() { 
   public void update(long pBytesRead, long pContentLength, int pItems) { 
    System.out.println("xȡļ " + pItems); 
    if (pContentLength == -1) { 
     System.out.println("x??" + pBytesRead + " ʣ0"); 
    } else { 
     System.out.println("ļ?? + pContentLength + " x?? + pBytesRead); 
    } 
   } 
  }; 
  upload.setProgressListener(progressListener); 
 
  //Qςļāya 
  upload.setHeaderEncoding("UTF-8"); 
  //ДύρĔǷςεĔ 
  if (!ServletFileUpload.isMultipartContent(request)) { 
   //Ղyʽ@ȡ 
   return; 
  } 
 
  //Oς΂ļĴСֵĿǰOÞ1024*1024ֹҲ1MB 
  upload.setFileSizeMax(1024 * 1024); 
  //Oςļֵֵ=ͬrςĶļĴСֵĺĿǰOÞ10MB 
  upload.setSizeMax(1024 * 1024 * 10); 
 
  try { 
   //ʹServletFileUploadςYصһList<FileItem>ÿһFileItemһFormεݔ 
   List<FileItem> items = upload.parseRequest(request); 
   Iterator<FileItem> iterator = items.iterator(); 
   while (iterator.hasNext()) { 
    FileItem item = iterator.next(); 
 
    //Дjspύ^Dzļ 
    if (item.isFormField()) { 
     errorMessage = "Ոύļ"; 
     break; 
    } else { 
     //ļ 
     String fileName = item.getName(); 
     if (fileName == null || fileName.trim() == "") { 
      System.out.println("ļ"); 
     } 
     //̎ͬg[ύļ·} 
     fileName = fileName.substring(fileName.lastIndexOf("\\") + 1); 
     //ļUչ 
     String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1); 
     //ДUչǷϷ 
     if (!validExtension(fileExtension)) { 
      errorMessage = "ςļǷ"; 
      item.delete(); 
      break; 
     } 
     //@ļݔ 
     InputStream in = item.getInputStream(); 
     //õļQ 
     String saveFileName = createFileName(fileName); 
     //õļ· 
     String realFilePath = createRealFilePath(savePath, saveFileName); 
     //ļݔ 
     FileOutputStream out = new FileOutputStream(realFilePath); 
     //_^ 
     byte buffer[] = new byte[1024]; 
     int len = 0; 
     while ((len = in.read(buffer)) > 0) { 
      //ļ 
      out.write(buffer, 0, len); 
     } 
     //P]ݔ 
     in.close(); 
     //P]ݔ 
     out.close(); 
     //hRrļ TODO 
     item.delete(); 
     //ςļϢ浽 TODO 
    } 
 
   } 
 
  } catch (FileUploadBase.FileSizeLimitExceededException e) { 
   e.printStackTrace(); 
   request.setAttribute("errorMessage", "΂ļֵ??"); 
   request.getRequestDispatcher("pages/upload/upload.jsp").forward(request, response); 
   return; 
  } catch (FileUploadBase.SizeLimitExceededException e) { 
   e.printStackTrace(); 
   request.setAttribute("errorMessage", "ςļĿĴСƵֵ??"); 
   request.getRequestDispatcher("pages/upload/upload.jsp").forward(request, response); 
   return; 
  } catch (FileUploadException e) { 
   e.printStackTrace(); 
   request.setAttribute("errorMessage", "ļςʧ??"); 
   request.getRequestDispatcher("pages/upload/upload.jsp").forward(request, response); 
   return; 
  } 
 
  request.setAttribute("errorMessage", errorMessage); 
  request.getRequestDispatcher("pages/upload/upload.jsp").forward(request, response); 
 
 
 } 
 
 @Override 
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
  doGet(request, response); 
 } 
 
 private boolean validExtension(String fileExtension) { 
  String[] exts = {"jpg", "txt", "doc", "pdf"}; 
  for (int i = 0; i < exts.length; i++) { 
   if (fileExtension.equals(exts[i])) { 
    return true; 
   } 
 
  } 
 
  return false; 
 } 
 
 private String createFileName(String fileName) { 
  return UUID.randomUUID().toString() + "_" + fileName; 
 } 
 
 /** 
  * ·ļQ挍ļ··\\\\\\fileName 
  * 
  * @param basePath 
  * @param fileName 
  * @return 
  */ 
 private String createRealFilePath(String basePath, String fileName) { 
  Calendar today = Calendar.getInstance(); 
  String year = String.valueOf(today.get(Calendar.YEAR)); 
  String month = String.valueOf(today.get(Calendar.MONTH) + 1); 
 
 
  String upPath = basePath + File.separator + year + File.separator + month + File.separator; 
  File uploadFolder = new File(upPath); 
  if (!uploadFolder.exists()) { 
   uploadFolder.mkdirs(); 
  } 
 
  String realFilePath = upPath + fileName; 
 
  return realFilePath; 
 } 
} 

IJ

//localhost:8080/helloweb/pages/upload/upload.jsp




ϾDZĵȫϣҵČWҲϣҶ֧_֮

ܸdȤ:

  • Jsp+ServletFļςd ļбչʾ
  • JSP servletFļςd̈́h
  • jsp+servletFļςcd
  • Jsp+ServletFļςd hςļ

P

  • Jsp+ServletFļςd ļςһ

    Jsp+ServletFļςd ļςһ

    ļςdJava Web؂似܌ ʹõApacheµļςM org.apache.commons.fileupload F Y
    2019-08-25
  • python+selenium selectxλ̎

    python+selenium selectxλ̎

    һǰ Yһpython+selenium selectxλ̎ăɷNʽԂmʹÕrԃ ֱӶλXPath ʹFirebugҵҪλԪֱ
    2019-08-25
  • Jsp+ServletFļςd ļбչʾ

    Jsp+ServletFļςd ļбչʾ

    һƪv Jsp+ServletFļςdһ--ļς 팍FһςļбչʾͬrһµһеĴa UԒfϴa
    2019-08-25
  • python+selenium ¼

    python+selenium ¼

    һǰ ˿ʹ click( ) ģM˵ĆΓFWebaƷ߀ṩ˸S˽ʽIpͣτӵȹWebDriver
    2019-08-25
  • JavaʹJWTTokenMнӿbF

    JavaʹJWTTokenMнӿbF

    ȽBJWTMb˼· 1ÑlՈ 2ն˄һܺJWTϢToken 3ںmՈJWTϢՈ^loն
    2019-08-25
  • Jsp+ServletFļςd hςļ

    Jsp+ServletFļςd hςļ

    һƪvJsp+ServletFļςd--ļбչʾ 팍Fһ„hςļͬrһµһеĴa UԒfϴa
    2019-08-25
  • PythonLƹƱƄӾČ

    PythonLƹƱƄӾČ

    1. ǰ ƄӾǹƱMָIJnumpy.convolveӋƱƄӾ 2. numpy.convolve numpy.convolve(a, v, mode='full') Returns the discre
    2019-08-25
  • spring bootFCa

    spring bootFCa

    Čҷspring bootFCaܵľwa҅w ǰս 1.controllerӴa@ȡCaԼC
    2019-08-25
  • JSPF͑Ϣϵy

    JSPF͑Ϣϵy

    ČҷJSPF͑Ϣϵyľwa҅w ĿʾD@ӰԼ 䛽a index.jsp: ȫ]
    2019-08-25
  • JavaДMcϵ໥DQF

    JavaДMcϵ໥DQF

    @ƪҪBJavaДMcϵ໥DQF,ͨ^ʾaBķdzԔҵČW߹һąWrֵ,Ҫѿԅ
    2019-08-25