java 구성 요소 smartupload 파일 업로드 기능 구현

jsp와serlvet를 사용하여 가장 간단한 업로드를 실현하여 참고하시기 바랍니다. 구체적인 내용은 다음과 같습니다.
1. 페이지 index.jsp

<%@ page language="java" pageEncoding="UTF-8"%> 
<% 
String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
%> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
 <head> 
 <base href="<%=basePath%>"> 
 <title>index.jsp</title> 
 <meta http-equiv="pragma" content="no-cache"> 
 <meta http-equiv="cache-control" content="no-cache"> 
 <meta http-equiv="expires" content="0"> 
 </head> 
 <body> 
 <form action="upload" method="post" enctype="multipart/form-data"> 
 <br> 
  :<input type="text" name="uname"/> 
 <br> 
  :<input type="file" name="pic"/> 
 <br> 
 <input type="submit" value=" "></input> 
 </form> 
 </body> 
</html> 
2. 액션은 업로드의 servlet으로 넘어가기 때문에 웹을 원합니다.xml에 설정, 웹.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
 <servlet> 
 <servlet-name>uploadAction</servlet-name> 
 <servlet-class>com.pop.action.SmartuploadAction</servlet-class> 
 </servlet> 
 <servlet-mapping> 
 <servlet-name>uploadAction</servlet-name> 
 <url-pattern>/upload/*</url-pattern> 
 </servlet-mapping> 
 <welcome-file-list> 
 <welcome-file>index.jsp</welcome-file> 
 </welcome-file-list> 
</web-app> 
3, 매핑된 action 파일, SmartuploadAction.java:

package com.pop.action; 
 
import java.io.IOException; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.jsp.JspFactory; 
import javax.servlet.jsp.PageContext; 
 
import com.soft4j.httpupload4j.Request; 
import com.soft4j.httpupload4j.SmartUpload; 
import com.soft4j.httpupload4j.SmartUploadException; 
 
public class SmartuploadAction extends HttpServlet { 
 private static final long serialVersionUID = -8610555375032925108L; 
 @Override 
 protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
  throws ServletException, IOException { 
 req.setCharacterEncoding("utf-8"); 
 resp.setCharacterEncoding("utf-8"); 
 SmartUpload su = new SmartUpload(); 
 //  multipart/form-data req , smartupload request 
 Request reqest = su.getRequest(); 
 //  pageContext  
 PageContext pageContext = JspFactory.getDefaultFactory() 
  .getPageContext(this, req, resp, null, true, 8192, true); 
 su.initialize(pageContext); 
 try { 
  su.upload(); 
  //  upload  
  su.save("upload"); 
 } catch (SmartUploadException e) { 
  e.printStackTrace(); 
 } 
 //  smartupload reqest  
 String uname = reqest.getParameter("uname"); 
 System.out.println(uname); 
 } 
} 
마지막 설명: 사용하는 구성 요소 패키지는smartupload입니다.zip.
이상은 본문의 전체 내용입니다. 여러분의 학습에 도움이 되고 저희를 많이 응원해 주십시오.

좋은 웹페이지 즐겨찾기