java組件smartupload實現上傳檔案功能_java

來源:互聯網
上載者:User

使用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、action跳轉到了upload的servlet,所以要web.xml裡面配置,web.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。

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.