java組件smartupload實(shí)現(xiàn)上傳文件功能
使用jsp和serlvet來(lái)實(shí)現(xiàn)最簡(jiǎn)單的上傳,供大家參考,具體內(nèi)容如下
1、頁(yè)面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跳轉(zhuǎn)到了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的傳輸原因?qū)е聄eq不能使用,所以使用smartupload產(chǎn)生的request Request reqest = su.getRequest(); // 獲得pageContext對(duì)象 PageContext pageContext = JspFactory.getDefaultFactory() .getPageContext(this, req, resp, null, true, 8192, true); su.initialize(pageContext); try { su.upload(); // 上傳到本項(xiàng)目的upload目錄 su.save("upload"); } catch (SmartUploadException e) { e.printStackTrace(); } // 使用smartupload產(chǎn)生的reqest對(duì)象來(lái)獲得頁(yè)面?zhèn)鬟f的參數(shù) String uname = reqest.getParameter("uname"); System.out.println(uname); } }
最后說(shuō)明:使用的組件包為smartupload.zip。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- java基于servlet使用組件smartUpload實(shí)現(xiàn)文件上傳
- java使用smartupload組件實(shí)現(xiàn)文件上傳的方法
- Java組件commons fileupload實(shí)現(xiàn)文件上傳功能
- java組件commons-fileupload文件上傳示例
- java組件fileupload文件上傳demo
- JavaWeb實(shí)現(xiàn)文件上傳下載功能實(shí)例解析
- java中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例解析
- Java中FTPClient上傳中文目錄、中文文件名亂碼問(wèn)題解決方法
- java實(shí)現(xiàn)文件上傳下載和圖片壓縮代碼示例
- java使用ftp上傳文件示例分享
相關(guān)文章
springboot 實(shí)現(xiàn)bean手動(dòng)注入操作
這篇文章主要介紹了springboot 實(shí)現(xiàn)bean手動(dòng)注入操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01JAVA編程實(shí)現(xiàn)隨機(jī)生成指定長(zhǎng)度的密碼功能【大小寫(xiě)和數(shù)字組合】
這篇文章主要介紹了JAVA編程實(shí)現(xiàn)隨機(jī)生成指定長(zhǎng)度的密碼功能,可生成帶有大小寫(xiě)和數(shù)字組合的隨機(jī)字符串,需要的朋友可以參考下2017-07-07Mybatis RowBounds 限制查詢條數(shù)的實(shí)現(xiàn)代碼
Oracle 數(shù)據(jù)庫(kù)查詢?cè)黾覴owBounds限制查詢條數(shù),默認(rèn)是0到1000條。下面給大家分享Mybatis RowBounds 限制查詢條數(shù)的實(shí)現(xiàn)代碼,需要的朋友參考下吧2016-11-11Java如何正確處理下載文件時(shí)HTTP頭的編碼問(wèn)題
這篇文章主要介紹了Java如何正確處理下載文件時(shí)HTTP頭的編碼問(wèn)題,2023-07-07
通常HTTP消息包括客戶機(jī)向服務(wù)器的請(qǐng)求消息和服務(wù)器向客戶機(jī)的響應(yīng)消息,今天來(lái)講解下正確處理下載文件時(shí)HTTP頭的編碼問(wèn)題,需要的朋友可以參考下java實(shí)現(xiàn)socket客戶端連接服務(wù)端
本文是個(gè)人剛剛開(kāi)始學(xué)習(xí)如何通過(guò)socket去發(fā)送信息下邊的案例,也是書(shū)上的在這留下筆記,最后附上一個(gè)實(shí)例,有需要的小伙伴可以參考下。2015-10-10java并發(fā)請(qǐng)求下數(shù)據(jù)插入重復(fù)問(wèn)題的解決方法
現(xiàn)在遇到一個(gè)項(xiàng)目,移動(dòng)設(shè)備存儲(chǔ)數(shù)據(jù),然后一起上傳,那就出現(xiàn)了許多重復(fù)數(shù)據(jù),這篇文章主要給大家介紹了關(guān)于java并發(fā)請(qǐng)求下數(shù)據(jù)插入重復(fù)問(wèn)題的解決方法,需要的朋友可以參考下2021-11-11解析Java?中for循環(huán)和foreach循環(huán)哪個(gè)更快
這篇文章主要介紹了Java中for循環(huán)和foreach循環(huán)哪個(gè)更快示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09SpringMVC基于注解方式實(shí)現(xiàn)上傳下載
本文主要介紹了SpringMVC基于注解方式實(shí)現(xiàn)上傳下載,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04