Rumah  >  Artikel  >  hujung hadapan web  >  Pelaksanaan HTML5 bagi pelbagai fail muat naik contoh kod_html5 kemahiran tutorial

Pelaksanaan HTML5 bagi pelbagai fail muat naik contoh kod_html5 kemahiran tutorial

WBOY
WBOYasal
2016-05-16 15:48:251451semak imbas

Perkara utama yang digunakan ialah atribut berganda bagi 🎜>

Salin kod
Kodnya adalah seperti berikut:

;

Penyelesaian 4-5: berbilang fail
< /head> UploadPhotoServlet" ENCTYPE="multipart/form-data" METHOD="POST">
type="submit" value ="Submit"> >
Kod terperinci latar belakang java:
Salin kod


Kod tersebut adalah seperti berikut:

導入java.io.File;
導入java.io.IOException;
導入java.text.SimpleDateFormat;
導入java.util.Calendar;
導入java.utilil .Date;
導入java.util.List;
導入javax.servlet.ServletException;
導入javax.servlet.http.HttpServlet;
導入javax.servlet.http.HttpServletRequest;導入javax.servlet.http.HttpServletResponse;
導入org.apache.commons.fileupload.FileItem;
導入org.apache.commons.fileupload.FileUploadExfileion;
導入org.apache.commons.fileupload.FileUploadExfileion;
導入org.apache.commons.upload.commons.upload. .DiskFileItemFactory;
導入org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
* Servlet實作類別UploadPhotoServlet
*/
public class UploadPhotoServlet extends HttpServlet {
public class UploadPhotoServlet extends HttpServlet {
Vated staticic 585s serial;
/**
* @see HttpServlet#HttpServlet()
*/
public UploadPhotoServlet() {
super();
// TODO 自動產生的建構子存根
}
/**
* @see HttpServlet#doGet(HttpServletRequest 請求,HttpServletResponse 回應)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO自動產生的方法存根
多>; **
* @see HttpServlet#doPost(HttpServletRequest 請求,HttpServletResponse 回應)
*/
@SuppressWarnings("unchecked")
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, HttpServletResponse response) throws ServletException, IOttpServletResponse response) throws ServletException, IOttpServlet, getEachDate() "\";//按日期產生資料夾
File uploadPath=new File(imagePath);
if(!uploadPath.exists()){
uploadPath.mkdirs();
}
檔案tmp=new File("c:\tmp\");
if(!tmp.exists()){
tmp.mkdirs();
}
DiskFileItemyfactory =new DiskFileItemFactory ();//建立磁碟工廠
factory.setRepository(tmp);//設定檔案快取路徑
factory.setSizeThreshold(10 * 1096 );//將檔案儲存在記憶體或暫存檔案夾的預設臨界值,值為10240,即10kb
ServletFileUpload sfu=new ServletFileUpload(factory);//建立處理工具
sfu.setSizeMax(10*1024*1024 );//伺服器端可以接收的最大檔案大小,-1表示無上限
String fileName=null;
嘗試{
清單; list=sfu.parseRequest(request);//解析
if(list.size ()return;
}
for(int j=0;jFileItem item=list.get(j);
fileName=item.getName();
if(fileName.equals("")){
request.getRequestDispatcher("/com/visualizerPhoto.jsp").forward(請求,回應);
返回;
}
int pos=fileName.lastIndexOf(".");//取得圖片檔案格式
if(pos>0){
Date date=new Date();
fileName=imagePath date.getTime() fileName.substring(pos);
}
System.out.println("item:" item);
item.write(new File(fileName)) ;//寫入磁碟
}
} catch (FileUploadException e) {
e.printStackTrace();
} catch (異常e) {
e.printStackTrace();
}
}
// 13-11-15
public static String getEachDate() {
日曆cal = Calendar.getInstance();
cal.add(日曆.DATE, 00 );
字串昨天= new SimpleDateFormat("yyyy-MM-dd ").format(cal
.getTime());
String[] 日期=昨天.split("-");
String realDate = 日期[0].substring(2, 4) "-" 日期[1] "-"
日期[2];
回傳realDate.trim();
}
}


下面是效果圖:

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn