導航:首頁 > 編程語言 > 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圖片上傳相關的資料

熱點內容
fgets空文件 瀏覽:857
傳奇物品文件夾 瀏覽:352
word插入excel圖表 瀏覽:690
xp系統搜索不到本地文件 瀏覽:39
什麼網站可以找到拼團去西藏 瀏覽:247
javatask 瀏覽:50
MFC的文件名 瀏覽:972
cad自動保存的文件怎麼關閉 瀏覽:667
zip文件密碼多少 瀏覽:486
編程培訓哪些好 瀏覽:324
如何把文件夾內容變成文檔 瀏覽:509
小火箭幼兒編程怎麼拿積分 瀏覽:158
火車票時刻表及票價資料庫 瀏覽:94
求平均值java 瀏覽:767
linux如何分屏顯示 瀏覽:257
手機可以直接填寫pdf文件么 瀏覽:554
linux虛擬光碟機軟體 瀏覽:836
米2s最好的版本 瀏覽:640
小米6檢測工具下載 瀏覽:540
桌面創建不了文件夾怎麼回事 瀏覽:894

友情鏈接