這篇文章將為大家詳細(xì)講解有關(guān)J2EE如何實(shí)現(xiàn)Servlet上傳文件到服務(wù)器并相應(yīng)顯示功能,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)公司專(zhuān)業(yè)為企業(yè)提供南漳網(wǎng)站建設(shè)、南漳做網(wǎng)站、南漳網(wǎng)站設(shè)計(jì)、南漳網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、南漳企業(yè)網(wǎng)站模板建站服務(wù),十年南漳做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
編輯上傳文件的頁(yè)面upload.html
注意事項(xiàng):上傳方式使用POST不能使用GET(GET不能上傳文件)
表單 enctype 屬性應(yīng)該設(shè)置為 multipart/form-data.(表示提交的數(shù)據(jù)是二進(jìn)制文件)
upload.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>文件上傳</title> </head> <body> <form action="UploadPhotoServlet" method="POST" enctype="multipart/form-data"> 人物名稱(chēng):<input type="text" name="heroName"/><br> 上傳頭像:<input type="file" name="filepath"/><br> <input type="submit" value="上傳"> </form> </body> </html>
UPloadPtotoServlet文件上傳類(lèi)--上傳功能的開(kāi)發(fā)
將commons-io-1.4.jar和commons-fileupload-1.2.2.jar 兩個(gè)jar包放到WEB-INF/lib 目錄下。
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
* Servlet implementation class UploadPhotoServlet
*/
@WebServlet("/UploadPhotoServlet")
public class UploadPhotoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public UploadPhotoServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// response.getWriter().append("Served at: ").append(request.getContextPath());
String filename=null;
DiskFileItemFactory factory=new DiskFileItemFactory(); //磁盤(pán)文件條目工廠(chǎng)
ServletFileUpload upload=new ServletFileUpload(factory); //負(fù)責(zé)處理上傳的文件數(shù)據(jù),并將表單中每個(gè)輸入項(xiàng)封裝成一個(gè)fileitem對(duì)象中
//設(shè)置上傳文件的大小為10M
factory.setSizeThreshold(2*1024*1024);
List items=null;
try {
//parse 解析
items=upload.parseRequest(request); //得到一個(gè)保存了所有上傳內(nèi)容的List對(duì)象
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Iterator iter=items.iterator(); //迭代上傳的文件數(shù)據(jù)
while(iter.hasNext()){
FileItem item=(FileItem) iter.next();
if(!item.isFormField()){ //如果不是上傳的
//根據(jù)時(shí)間戳創(chuàng)建頭像文件
filename=System.currentTimeMillis()+".jpg";
//通過(guò)getrealpath獲取上傳文件夾,如果項(xiàng)目存在將存在當(dāng)前項(xiàng)目下 不存在的話(huà)創(chuàng)建項(xiàng)目文件夾
//圖片文件夾
String photoFolder=request.getServletContext().getRealPath("uploaded");
File f=new File(photoFolder,filename);
f.getParentFile().mkdirs(); //如果父文件夾不存在則自動(dòng)創(chuàng)建
//通過(guò)item.getInputStream() 獲取瀏覽器上傳的文件
InputStream is = item.getInputStream(); //將文件讀進(jìn)來(lái)
//復(fù)制文件
FileOutputStream fos=new FileOutputStream(f); //往界面上顯示
byte[] b=new byte[2*1024*1024];
int len=0;
while((len=is.read(b))!=-1){
fos.write(b, 0, len);
}
fos.close();
}else{
System.out.println(item.getFieldName());//heroName
String value=item.getString();
value=new String(value.getBytes("ISO-8859-1"), "UTF-8");
System.out.println(value); //桑葚
}
}
String html="<img width='200' height='150' src='uploaded/%s'/>";
response.setContentType("text/html");
PrintWriter pw=response.getWriter();
pw.format(html, filename);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}運(yùn)行結(jié)果:

關(guān)于“J2EE如何實(shí)現(xiàn)Servlet上傳文件到服務(wù)器并相應(yīng)顯示功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
分享標(biāo)題:J2EE如何實(shí)現(xiàn)Servlet上傳文件到服務(wù)器并相應(yīng)顯示功能
地址分享:http://www.jbt999.com/article16/gjsddg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開(kāi)發(fā)、響應(yīng)式網(wǎng)站、定制網(wǎng)站、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、小程序開(kāi)發(fā)、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)