文件上傳是網(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)目路徑下面。
使用代碼如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
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無(wú)上限 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ì)將完整路徑名傳過(guò)來(lái) 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ù)庫(kù)中 } } } catch (Exception e) { e.printStackTrace(); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } |
這里分別判斷是否是上傳的流或者表單里面的參數(shù),比如文本框提交信息,然后將他們插入到數(shù)據(jù)庫(kù)中。數(shù)據(jù)庫(kù)插入
代碼如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
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ù)庫(kù)中 */ public void insert(InputStream in, String fileName, String type,String describe) throws Exception{ //向數(shù)據(jù)庫(kù)中寫入圖片 Connection conn = null ; PreparedStatement ps = null ; ResultSet rs = null ; System.out.println(describe); try { // 2.建立連接 conn = JdbcUtils.getConnection(); // 3.創(chuàng)建語(yǔ)句 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í)行語(yǔ)句 ps.executeUpdate(); in.close(); } finally { JdbcUtils.free(rs, ps, conn); } } } |
可能會(huì)遇到數(shù)據(jù)庫(kù)默認(rèn)問(wèn)價(jià)大小限制,需要在mysql安裝目錄下面的my.ini下面更改如下配置,
[mysqld]
max_allowed_packet=64M
這樣就可以了。當(dāng)然,注意編碼格式。上傳文件搞定。還有就是我的一個(gè)列名設(shè)置為describe,結(jié)果和Mysql保留字沖
突,出現(xiàn)無(wú)法插入信息現(xiàn)象,以后一定要注意。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。