Komentar: Mengunggah banyak file mungkin rumit untuk diimplementasikan di masa lalu, tetapi sejak munculnya HTML5, itu menjadi sangat mudah. Ada contoh yang bagus di bawah ini, Anda dapat merujuknya.
Penggunaan utama adalah beberapa atribut dari <sput><input type = "file" ganda />
Berikut adalah kode terperinci dari halaman ini:
<! Doctype html>
<Html>
<head>
<meta http-equiv = "konten-tipe" content = "text /html; charset = gb2312" />
<title> Solusi 4-5: Mengirim beberapa file </iteme>
</head>
<body>
<form
Action = "http://10.10.25.31:8080/myupload/uploadphotoServlet" enctype = "multipart/form-data" method = "post">
<input type = "file" beberapa /> <input
type = "kirim" value = "kirim"> <input type = "reset" value = "reset">
</form>
</body>
</html>
Kode terperinci latar belakang Java:
impor java.io.file;
impor java.io.ioException;
impor java.text.simpledateFormat;
impor java.util.calendar;
impor java.util.date;
impor java.util.list;
impor javax.servlet.servletException;
impor javax.servlet.http.httpservlet;
impor javax.servlet.http.httpservletRequest;
impor javax.servlet.http.httpservletResponse;
impor org.apache.commons.fileupload.fileItem;
impor org.apache.commons.fileupload.fileuploadexception;
impor org.apache.commons.fileupload.disk.diskfileItemFactory;
impor org.apache.commons.fileupload.servlet.servletfileupload;
/**
* Kelas Implementasi Servlet UnggahphotoServlet
*/
PUBLIK UPLOADPHOTOSERVLET memperluas httpservlet {
private static final long serialversionuid = 1L;
/**
* @see httpservlet#httpservlet ()
*/
Public unggahanPhotOserVlet () {
super();
// TODO Stub Konstruktor yang dihasilkan otomatis
}
/**
* @See httpservlet#doGet (permintaan httpservletRequest, respons httpservletResponse)
*/
DOGET VOID DOGET (HTTPSERVLETREquest Request, HTTPSerVletResponse Response) melempar ServletException, IoException {
// TODO Stub Metode yang dihasilkan otomatis
dopost (permintaan, respons);
}
/**
* @See httpservlet#dopost (permintaan httpservletrequest, respons httpservletresponse)
*/
@SuppressWarnings ("Uncecked")
Dopost void yang dilindungi (permintaan httpservletRequest, respons httpservletResponse) melempar servletException, ioException {
String ImagePath = "c: // unggahfile // gambar //"+geteachdate ()+"//"; // Hasilkan folder berdasarkan tanggal
File unggahPath = file baru (imagepath);
if (! unggahPath.exists ()) {
unggahpath.mkdirs ();
}
File tmp = file baru ("c: // tmp //");
if (! tmp.exists ()) {
tmp.mkdirs ();
}
DISKFILEITEMFACTORY FACTORY = DISKFILEITEMFACTORY baru (); // Buat pabrik disk
factory.setrepository (tmp); // atur jalur cache file
factory.setsizethreshold (10 * 1096); // Nilai kritis default menyimpan file dalam memori atau folder disk sementara adalah 10240, mis. 10kb
Servletfileupload sfu = servletfileupload baru (pabrik); // Buat alat pemrosesan
sfu.setsizeMax (10*1024*1024); // Ukuran file maksimum yang dapat diterima oleh server, -1 berarti tidak ada batas atas
String fileName = null;
mencoba {
Daftar <FILEITEM> DAFTAR = SFU.PARSEREQUEST (permintaan); // Analisis
if (list.size () <1) {
kembali;
}
untuk (int j = 0; j <list.size (); j ++) {
FileItem item = list.get (j);
fileName = item.getName ();
if (filename.equals ("")) {
request.getRequestDispatcher ("/com/visualzerphoto.jsp"). Forward (Request, Response);
kembali;
}
int pos = filename.lastIndexof ("."); // Dapatkan format file gambar
if (pos> 0) {
Tanggal Tanggal = Tanggal Baru ();
fileName = imagePath+date.getTime ()+filename.substring (POS);
}
System.out.println ("Item:"+item);
item.write (file baru (nama file)); // tulis ke disk
}
} catch (FileuploAdException e) {
e.printstacktrace ();
} catch (Exception e) {
e.printstacktrace ();
}
}
// 13-11-15
string statis publik geteachdate () {
Calendar cal = calendar.getInstance ();
Cal.Add (Calendar.Date, 0);
String kemarin = new SimpleDeFormat ("yyyy-mm-dd") .Format (Cal
.getTime ());
String [] tanggal = kemarin.split ("-");
String RealDate = Tanggal [0] .substring (2, 4) + "-" + Tanggal [1] + "-"
+ tanggal [2];
return realdate.trim ();
}
}
Ini renderingnya: