javaweb中上傳圖片並顯示圖片,用我要上傳課程信息(裏面包括照片)這個例子說明

原理:  從客戶端上傳到服務器                照片——文件夾——數據庫數據庫

例如:桌面一張照片,在tomacat裏建立upload文件夾,把桌面照片上傳到upload文件夾裏,而且把照片的名字取出來,取完名字把這個名字插入到數據庫裏面,下次要想取就取這個名字到upload文件夾下面去尋找這個照片,找到之後寫相對路徑,就能夠在頁面上顯示照片。數組

因此我數據庫的類型是照片的路徑是varchar字符串類型緩存

注:tomacat服務器是用eclipse敲代碼的開發工具啓動的,每個都會把最新的源代碼覆蓋原有的全部代碼,全部tomacat重啓後照片會消失(真正的開發是編碼和服務器不在一塊兒不會出現以上狀況)服務器

例如:我要上傳課程信息(裏面包括照片)eclipse

1.上傳課程信息jsp頁面uploadcourse.jspjsp

<body background="image/zzz.jpg">
<div id="zt" style="height:960px;width:960px">
<div id="upld" style="height:300px;width:300px;margin-left: 300px;margin-top: 100px;">
<table>
<form action="CourseServlet?method=upload" method="post" enctype="multipart/form-data">
<tr>
<td style="text-align:center;"colspan="2"><font size="5">上傳課程</font></td>
<tr height="40px">
<td><div style="width:100px">課程名稱:<div></td>
<td> <input type="text" name="name" id="name"></td>
</tr>
<tr height="40px">
<td>課程描述:</td>
<td><input type="text" name="detail" id="detail"></td>
</tr>
<tr height="40px">
<td>封面圖片:</td>
<td><input type="file" name="picture" id="picture"></td>
</tr>
<td height="40px">課程講師:</td>
<td><input type="text" name="teacher" id="teacher"></td>
</tr>
<tr height="40px">
<td><input type="reset" value="重置"></td>
<td colspan="2" style="padding-left: 120px;"><input type="submit" value="提交"></td>
</tr>
</form>
</table>
</div>
</div>
</body>ide

2. 點擊查看已上傳課程按鈕後觸發活動xyadmin.jsp工具

<div style="height:100px"><a href="CourseServlet?method=displayCourse"  target="middle">查看已上傳課程</a></div>post

3.顯示課程信息jsp頁面displayCourse.jsp開發工具

<table border="0"cellspacing="0" cellpadding="0">
<tr>
<td style="width:50px;text-align: center">序號</td>
<td style="width:100px;text-align: center">課程名</td>
<td style="text-align: center">課程講述</td>
<td style="text-align: center">封面圖片</td>
<td style="width:100px;text-align: center">課程講師</td>
<td style="width:100px;text-align: center">相關視頻</td>
</tr>
<c:forEach items="${list_displaycourse}" var="course" varStatus="i">
<tr style="background:#7FFFD4">
<form action="CheckVideoServlet?method=checkvideo" method="post" target="middle">
<td style="width:50px;text-align: center">${i.count} </td>
<input type="hidden" name="c_id" id="c_id" value="${course.c_id} ">
<td style="width:100px;text-align: center">${course.c_name}</td>
<td style="text-align: center"><font style="font-size:12px;">${course.c_detail}</font></td>
<td style="text-align: center"><img width="100px" height="50px" src="upload/${course.c_picture}"/></td>
<td style="width:100px;text-align: center">${course.c_teacher}</td>
<td style="text-align: center"><input type="submit" value="查看"></td>
</form>
</tr>
</c:forEach>

後臺CourseServlet(包含上傳課程信息與顯示課程信息兩個處理邏輯)

public void upload(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String filename = null;
// 得到磁盤文件條目工廠
DiskFileItemFactory factory = new DiskFileItemFactory();
// 獲取文件須要上傳到的路徑
String path = request.getRealPath("/upload");

// 若是沒如下兩行設置的話,上傳大的 文件 會佔用 不少內存,
// 設置暫時存放的 存儲室 , 這個存儲室,能夠和 最終存儲文件 的目錄不一樣
/**
* 原理 它是先存到 暫時存儲室,而後在真正寫到 對應目錄的硬盤上, 按理來講 當上傳一個文件時,實際上是上傳了兩份,第一個是以 .tem
* 格式的 而後再將其真正寫到 對應目錄的硬盤上
*/
factory.setRepository(new File(path));
// 設置 緩存的大小,當上傳文件的容量超過該緩存時,直接放到 暫時存儲室
factory.setSizeThreshold(1024 * 1024);

// 高水平的API文件上傳處理
ServletFileUpload upload = new ServletFileUpload(factory);
InputStream in =null;
byte[] buf=null;//字節數組表示照片
try {
// 能夠上傳多個文件
List<FileItem> list = (List<FileItem>) upload.parseRequest(request);

for (FileItem item : list) {
// 獲取表單的屬性名字
String name = item.getFieldName();// title

// 若是獲取的 表單信息是普通的 文本 信息
if (item.isFormField()) {
// 獲取用戶具體輸入的字符串 ,名字起得挺好,由於表單提交過來的是 字符串類型的,表示表單的普通文本,以下拉列表,文本框,密碼框等
String value = item.getString("UTF-8");// title content,設置格式防止出現亂碼不匹配的狀況
request.setAttribute(name, value);
}
// 對傳入的非 簡單的字符串進行處理 ,好比說二進制的 圖片,電影這些
else {
/**
* 如下三步,主要獲取 上傳文件的名字,表示文本是上傳控件
* 名字採用隨機的方式設置的
*/
// 獲取路徑名
String value = item.getName();
String suffix = value.substring(value.lastIndexOf("."));
filename = "pro"+String.valueOf(((new Date()).getTime())%10000000)+suffix;
request.setAttribute(name, filename);


// 真正寫到磁盤上
// 它拋出的異常 用exception 捕捉

// item.write( new File(path,filename) );//第三方提供的

// 手動寫的,是將我電腦裏的照片寫在我服務器創建的upload文件夾下下面
OutputStream out = new FileOutputStream(new File(path,
filename));

in = item.getInputStream();

int length = 0;
buf = new byte[1024];//讀1024個字節

System.out.println("獲取上傳文件的總共的容量:" + item.getSize());

// in.read(buf) 每次讀到的數據存放在 buf 數組中
while ((length = in.read(buf)) != -1) {
// 在 buf 數組中 取出數據 寫到 (輸出流)磁盤上
out.write(buf, 0, length);

}

in.close();
out.close();
}
}

} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
//以上servlet代碼除了紅字外其他都不變

String name=request.getAttribute("name").toString();
String detail=request.getAttribute("detail").toString();
String teacher=request.getAttribute("teacher").toString();

Course course=new Course();
course.setC_name(name);
course.setC_detail(detail);
course.setC_teacher(teacher);
course.setC_picture(filename);//核心代碼,把filename的字符串給video.setImages插入數據庫
boolean flag= courseService.uploadCourse(course);
if(flag){//上傳成功
request.getRequestDispatcher("xyadmin.jsp").forward(request, response);
}else{
request.getRequestDispatcher("uploadcourse.jsp").forward(request, response);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String method=request.getParameter("method");
if("upload".equals(method)){
upload(request,response);
}else if("displayCourse".equals(method)){ List<Course> listCourse=courseService.displayCourse(); request.setAttribute("list_displaycourse", listCourse); request.getRequestDispatcher("displayCourse.jsp").forward(request, response); } }

相關文章
相關標籤/搜索