java使用common-fileupload實(shí)現(xiàn)文件上傳
文件上傳是網(wǎng)站非常常用的功能,直接使用Servlet獲取上傳文件還得解析請(qǐng)求參數(shù),比較麻煩,所以一般選擇采用apache的開源工具,common-fileupload.這個(gè)jar包可以再apache官網(wǎng)上面找到,也可以在struts的lib文件夾下面找到,struts上傳的功能就是基于這個(gè)實(shí)現(xiàn)的。
common-fileupload是依賴于common-io這個(gè)包的,所以還需要下載這個(gè)包。然后導(dǎo)入到你的項(xiàng)目路徑下面。
使用代碼如下
package oop.hg.ytu.servlet; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import oop.hu.ytu.dao.UploadDomain; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; public class Upload extends HttpServlet { /** * 處理用戶上傳請(qǐng)求 */ private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // String describe = request.getParameter("describe"); DiskFileItemFactory factory = new DiskFileItemFactory(); @SuppressWarnings("deprecation") String path = request.getRealPath("/upload");//設(shè)置磁盤緩沖路徑 factory.setRepository(new File(path)); factory.setSizeThreshold(1024*1024);//設(shè)置創(chuàng)建緩沖大小 ServletFileUpload upload = new ServletFileUpload(factory); upload.setSizeMax(-1);//設(shè)置上傳文件限制大小,-1無上限 try { @SuppressWarnings("unchecked") List<FileItem> list = upload.parseRequest(request); String va = null; for(FileItem item : list){ // String name = item.getFieldName(); if(item.isFormField()){//判斷是否是文件流 va = item.getString("UTF-8"); // System.out.println(name+"="+va); /// request.setAttribute(name, value); }else{ String value = item.getName();//會(huì)將完整路徑名傳過來 int start = value.lastIndexOf("\\"); String fileName = value.substring(start+1); // request.setAttribute(name, fileName); InputStream in = item.getInputStream(); UploadDomain dao = new UploadDomain(); //item.write(new File(realPath,fileName)); int index = fileName.lastIndexOf("."); String realFileName = fileName.substring(0,index); String type = fileName.substring(index+1); dao.insert(in, realFileName,type,va);//放入到數(shù)據(jù)庫中 } } } catch (Exception e) { e.printStackTrace(); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
這里分別判斷是否是上傳的流或者表單里面的參數(shù),比如文本框提交信息,然后將他們插入到數(shù)據(jù)庫中。數(shù)據(jù)庫插入
代碼如下
package oop.hu.ytu.dao; import java.io.InputStream; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import oop.hg.ytu.utils.JdbcUtils; /** * 提供文件上傳支持 * @author Administrator * */ public class UploadDomain { /** * 將上傳的文件流放入到數(shù)據(jù)庫中 */ public void insert(InputStream in, String fileName, String type,String describe) throws Exception{//向數(shù)據(jù)庫中寫入圖片 Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; System.out.println(describe); try { // 2.建立連接 conn = JdbcUtils.getConnection(); // 3.創(chuàng)建語句 String sql = "insert into fileupload(file,filename,type,des) values (?,?,?,?)"; ps = conn.prepareStatement(sql); ps.setBlob(1, in); ps.setString(2, fileName); ps.setString(3, type); ps.setString(4, describe); // 4.執(zhí)行語句 ps.executeUpdate(); in.close(); } finally { JdbcUtils.free(rs, ps, conn); } } }
可能會(huì)遇到數(shù)據(jù)庫默認(rèn)問價(jià)大小限制,需要在mysql安裝目錄下面的my.ini下面更改如下配置,
[mysqld]
max_allowed_packet=64M
這樣就可以了。當(dāng)然,注意編碼格式。上傳文件搞定。還有就是我的一個(gè)列名設(shè)置為describe,結(jié)果和Mysql保留字沖
突,出現(xiàn)無法插入信息現(xiàn)象,以后一定要注意。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- java組件SmartUpload和FileUpload實(shí)現(xiàn)文件上傳功能
- Java中使用fileupload組件實(shí)現(xiàn)文件上傳功能的實(shí)例代碼
- java組件commons-fileupload實(shí)現(xiàn)文件上傳、下載、在線打開
- Java組件commons fileupload實(shí)現(xiàn)文件上傳功能
- JavaEE組件commons-fileupload實(shí)現(xiàn)文件上傳、下載
- java組件commons-fileupload文件上傳示例
- java組件fileupload文件上傳demo
- java組件commons-fileupload實(shí)現(xiàn)文件上傳
- JAVA使用commos-fileupload實(shí)現(xiàn)文件上傳與下載實(shí)例解析
- 使用fileupload組件實(shí)現(xiàn)文件上傳功能
相關(guān)文章
SpringSecurity自動(dòng)登錄流程與實(shí)現(xiàn)詳解
這篇文章主要介紹了SpringSecurity自動(dòng)登錄流程與實(shí)現(xiàn)詳解,所謂的自動(dòng)登錄是在訪問鏈接時(shí)瀏覽器自動(dòng)攜帶上了Cookie中的Token交給后端校驗(yàn),如果刪掉了Cookie或者過期了同樣是需要再次驗(yàn)證的,需要的朋友可以參考下2024-01-01Spring 整合 MyBatis的實(shí)現(xiàn)步驟
SpringMVC 本來就是 Spring 框架的一部分,這兩者無須再做整合,所以 SSM 整合的關(guān)鍵就是Spring對(duì)MyBatis的整合,三大框架整合完成后,將以 Spring 為核心,調(diào)用有關(guān)資源,高效運(yùn)作,這篇文章主要介紹了 Spring 整合 MyBatis的實(shí)現(xiàn)步驟,需要的朋友可以參考下2023-02-02SpringBoot實(shí)戰(zhàn)之處理異常案例詳解
這篇文章主要介紹了SpringBoot實(shí)戰(zhàn)之處理異常案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09java 中Spark中將對(duì)象序列化存儲(chǔ)到hdfs
這篇文章主要介紹了java 中Spark中將對(duì)象序列化存儲(chǔ)到hdfs的相關(guān)資料,需要的朋友可以參考下2017-06-06java根據(jù)開始時(shí)間結(jié)束時(shí)間計(jì)算中間間隔日期的實(shí)例代碼
這篇文章主要介紹了java根據(jù)開始時(shí)間結(jié)束時(shí)間計(jì)算中間間隔日期的實(shí)例代碼,需要的朋友可以參考下2019-05-05