導航:首頁 > 編程語言 > jsp圖片上傳

jsp圖片上傳

發布時間:2024-12-05 16:51:57

1. 怎麼樣在jsp頁面上傳圖片把圖片的名字如1.jpg上傳到資料庫,把圖片保存到一個制定的文件夾裡面。

在JSP頁面中用"<input type="file"/> "然後用form提交到後台 得到文件名然後在servlet裡面用輸入輸出流不就可以了。

2. 在jsp中做照片上傳預覽的代碼

建議你使用 Web upload 組件,可圖片預覽, 可多選,可拖放上傳,可粘貼後直接上傳

網路開源產品.

倆三行代碼就可以直接使用

具體網址 :

http://www.admin10000.com/document/4721.html

3. jsp+servlet 上傳圖片並顯示出來

其實你這個擋也顯示圖片其實很簡單的,
你的需求無非是兩個
1.servlet上傳文件(圖片)
2.點擊 瀏覽 圖標,然後選擇圖片文件,然後就可以在頁面中的某個地方看到圖片

是這兩個需求么?
首先說第二個吧。
你上傳圖片之後,就馬上觸發js函數,內容為
var PicPath = document.getElementById("yourfile").value;
document.getElementById("yourDiv").innerHTML="<IMG src="+PicPath+"/>";
OK了

第一個嘛就無所謂說了,不過我還是貼一個代碼吧,
public void upLoadFile(HttpServletRequest request, HttpServletResponse response) {
PrintWriter out = null;
response.setCharacterEncoding("UTF-8");
//實例化文件工廠
FileItemFactory factory = new DiskFileItemFactory();
//配置上傳組件ServletFileUpload
ServletFileUpload upload = new ServletFileUpload(factory);
try {
out = response.getWriter();
//從request得到所有上傳域的列表
List<FileItem> list = upload.parseRequest(request);

for (FileItem item : list) {
//isFormField判斷一個item類對象封裝的是一個普通的表單欄位還是文件表單欄位。
// 如果item是文件域,則做出如下處理:
if (!item.isFormField()) {

//上傳文件域的Name
String fileName = item.getName();

//截取擴展名
int idx = fileName.lastIndexOf(".");
String extension = fileName.substring(idx);

//獲取文件名
String name = new Date().getTime() + extension;

//得到文件夾的物理路徑
String path = this.getServletContext().getRealPath("\\upload");

//創建一個File
File file = new File(path + "\\" + name);
FileOutputStream o = new FileOutputStream(file);
InputStream in = item.getInputStream();
try {
LoadProcessServlet.process = 0;
LoadProcessServlet.total = 100;
LoadProcessServlet.isEnd = false;
LoadProcessServlet.total = item.getSize();
byte b[] = new byte[1024];
int n;
while ((n = in.read(b)) != -1) {
LoadProcessServlet.process+=n;
o.write(b, 0, n);
System.out.println("實際:"+LoadProcessServlet.process);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
LoadProcessServlet.isEnd = true;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}

}

4. jsp用upload實現圖片上傳時圖片不能顯示出來(沒有顯示錯誤)

程序開發中,盡量不要使用相對路徑,容易出問題。你的問題很好解決

在文件最頭上添加以下代碼

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
然後圖片路徑這樣寫
<img src="<%=basePath%>upload/<%=fileName%>" />

5. Java在jsp中 如何上傳圖片 在上傳時可以取到圖片大小並修改

用第三方工具去取 common-upload,具體取到圖片的方法參考代碼如下:
FileItemFactory fileItemFactory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(fileItemFactory);
upload.setHeaderEncoding("utf-8");
try {
List<FileItem> items = upload.parseRequest(request);
for (FileItem fileItem : items) {
System.out.println("fileName=" + fileItem.getFieldName());
//獲取文件流
InputStream in = fileItem.getInputStream();
ServletContext context = getServletConfig().getServletContext();
String path = context.getRealPath("image");
System.out.println(path);
OutputStream out = new FileOutputStream(new File(path + "\\" + fileItem.getName()));
byte[] buffer = new byte[1024];
int len = 0;
while((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.close();
in.close();
System.out.println("寫入完畢");
}
} catch (FileUploadException e) {
e.printStackTrace();
}

閱讀全文

與jsp圖片上傳相關的資料

熱點內容
怎麼把跳舞視頻弄進文件夾 瀏覽:173
音樂app哪些是騰訊的 瀏覽:626
qq惡搞暴走圖片製作 瀏覽:347
相對坐標編程有什麼特點和好處 瀏覽:785
網站反爬取怎麼辦 瀏覽:59
網路愛好者 瀏覽:259
電信卡登陸什麼app 瀏覽:625
中文數據怎麼導入matlab 瀏覽:667
6賺錢app有哪些 瀏覽:726
數控編程語句case是什麼意思 瀏覽:364
cad文件打開突然關了 瀏覽:993
怎麼將文件名復制到粘貼板 瀏覽:220
什麼網站看港漫好用 瀏覽:618
照相機視頻的文件夾 瀏覽:205
照片怎樣用cad轉成cad文件 瀏覽:822
有兩個網站如何操作 瀏覽:499
excel怎麼把兩個表格中的數據導入 瀏覽:530
什麼是一二一二三APP 瀏覽:877
cs6的魔棒工具 瀏覽:243
文件夾變成受系統保護 瀏覽:479

友情鏈接