當前位置:首頁 » 文件管理 » jsp上傳圖片並預覽

jsp上傳圖片並預覽

發布時間: 2023-07-10 23:25:30

① 通過JSP怎樣上傳圖片到伺服器

1.限制文件上傳類型只能是圖片
function checkFileType(name,file){
var extArray = new Array(".doc",".docx");
var allowSubmit = false;
if (!file){
return;
}
while (file.indexOf("\\") != -1){
file = file.slice(file.indexOf("\\") + 1);
}
var ext = file.slice(file.indexOf(".")).toLowerCase();
for (var i = 0; i < extArray.length; i++) {
if (extArray[i] == ext){
allowSubmit = true;
break;
}
}
if(!allowSubmit){
alert("只能上傳以下格式的文件:"+ (extArray.join("")) + "\n請重新選擇再上傳.");
document.getElementById(name).value = "";
}
}
其中:extArray是要求文件類型。可自行定義。

2.引入jQuery外部文件
jquery-2.1.4.min.js
3.編寫js代碼
$(function () {
$('#txtfilePath1').uploadReview({
width: 350,
height: 350,
target: '#uploadReview1_content'
});
});
其中:txtfilePath1是input:file。width,height是預覽圖片的寬度和高度。target是顯示預覽圖片的位置。

4.編寫jsp頁面代碼
<body>
<input type="text" class="yourClassName" name="filePath1" id="filePath1"/>
<input type="file" id="txtfilePath1" name="txtfilePath1" style="display:none;">
<input type="button" onclick="txtfilePath1.click()" id="fileup1" name="fileup1" class="searchThing"value="上傳">
</body>

註: 這個是很久以前在網上看到的,就整理了下來,但是這么久都沒用過,所以也沒調試過,你自己試一試研究研究, 再來網上很多博客里,他們寫的很詳細的,可以去看看

② 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();
}

}

③ 求大神,jsp上上傳圖片,並顯示出來,將其相對路徑記入資料庫

java">

Stringtime=newSimpleDateFormat("yyyyMMddHHmmss")

.format(Calendar.getInstance().getTime());//得到系統時間

//上傳技術

SmartUploap=newSmartUpload();


//進行初始化


up.initialize(this.getServletConfig(),request,response);


//開始上傳


try{

up.upload("utf-8");//設置編碼方式。

intid=Integer.parseInt(up.getRequest().getParameter("id"));//商品編號

SmartFilessf=up.getFiles();//得到上傳的所有圖片

SmartFilefile=sf.getFile(0);//根據索引得到上傳圖片多個圖片可以用循環:

Stringtype=file.getFileExt();//得到圖片後綴名

Stringfolder="tp/";//指定文件夾

Stringpath=folder+time+"."+type;//路徑

System.out.println(path+"路徑");

file.saveAs(request.getRealPath("/")+path);//保存圖片


}catch(Exceptione){

e.printStackTrace();

}

//你搞個郵箱我把SmartUploadjar包發給你吧。 //設置from提交

/*<form action="SellerServet" method="post"

enctype="multipart/form-data">*/ // 加上enctype="multipart/form-data

④ 用Java Web的jsp製作圖片上傳和顯示如何實現

用jspSmartUpload組件來實現,用jsp+servlet在Servlet里實現的代碼:

PrintWriter out = response.getWriter();
int count = 0;
// 實例化虧友上傳控制項對象
SmartUpload su = new SmartUpload();
// 初始化操作
su.initialize(config, request, response);

// 設置上傳文件最大位元組數
su.setTotalMaxFileSize(100000);

//
try {
//禁止上傳指定擴展和空喚名的文件
su.setDeniedFilesList("ext,bat,jsp");
} catch (SQLException e1) {
e1.printStackTrace();
}

try {
// 上傳文件到伺服器
su.upload();

File fileup = new File(request.getRealPath("upload"));
if(!fileup.exists()){
// 創建目錄
fileup.mkdir();
}
// 處理多個文件的上傳
for(int i = 0;i < su.getFiles().getCount();i++){
com.jspsmart.upload.File file = su.getFiles().getFile(i);
if(!file.isMissing()){ // 如果文件有效
// 保存文件到指定上傳目錄
file.saveAs("/upload/new."+file.getFileExt(), su.SAVE_VIRTUAL);
count = su.save("/upload");
}
}

} catch (SmartUploadException e) {

e.printStackTrace();
}
out.println(count +"file(s) uploaded");

如果你對這個上喚凱傳組件不了解,最好是先去查查用法。。。

⑤ JSP如何上傳圖片

如果你是純JSP寫的,可以用SmartUpload.在你的頁面form 里 <form action="doUpload.jsp" method="POST" enctype="multipart/form-data">
文件名:<input type="text" name="name"/><br>
請選擇上傳的文件:<input type="file" name="file1"/>
<input type="submit" value="上傳"/>
</form> 注意:enctype="multipart/form-data"這個一定要這樣設置,具體什麼意思我也不是很清楚.....(呵呵) 提交到執行的頁面如下: //實例化上傳組件
SmartUpload upload = new SmartUpload();
//初始化上傳組件
upload.initialize(this.getServletConfig(), request, response);
//開始上傳
upload.upload();
//獲取上傳的文件列表對象
Files f = upload.getFiles();
//獲取文件對象
File fil = f.getFile(0);
//去掉文件後綴
String ext = fil.getFileExt();
//判斷文件類型是否是jpg格式jpg,gif,bmp,png,JPG,GIF,BMP,PNG
if (!(ext.equals("jpg")) && !(ext.equals("gif")) && !(ext.equals("bmp")) && !(ext.equals("png")) && !(ext.equals("JPG")) && !(ext.equals("GIF")) && !(ext.equals("BMP")) && !(ext.equals("PNG"))) {
out.println("<script type='text/javascript'>alert('文件類型錯誤');location.replace('upLoadPhoto.jsp');</script>");
return;
}
//滿足條件進行文件的上傳uploadImages在webRoot文件夾下的一個目錄
fil.saveAs("uploadImages/" + fil.getFileName());
String filepath = "uploadImages/" + fil.getFileName(); //保存到資料庫的路徑 OK.這樣就可以了.....

⑥ jsp上傳及顯示照片

<form action="pages/news/NewsServlet?status=insert" method="post" enctype="multipart/form-data">
新聞圖片:
<input type="file" name="img">
<br />
<input type="submit" value="添加">
</form>
後台代碼
public void insert(HttpServletRequest request, HttpServletResponse response)
throws Exception {
SmartUpload smart = new SmartUpload();
smart.initialize(super.getServletConfig(), request, response);
smart.upload();

File img = smart.getFiles().getFile(0);
String fileName = null;
if (img != null && img.getSize() > 0) {
fileName = UUID.randomUUID().toString() + "." + img.getFileExt();

img.saveAs("/upload/" + fileName);
}

String title = smart.getRequest().getParameter("title");
String content = smart.getRequest().getParameter("content");

News news = new News();
news.setTitle(title);
news.setContent(content);
news.setImg(fileName);
news.setPostDate(new Date());

boolean flag = ServiceFactory.getINewsServiceInstance().insert(news);

if (flag) {
request.setAttribute("message", "新聞添加成功");
} else {
request.setAttribute("message", "新聞添加失敗");
}
request.setAttribute("url", "pages/news/NewsServlet?status=list");

request.getRequestDispatcher("/pages/forward.jsp").forward(request,
response);
}
}

⑦ jsp怎麼實現瀏覽本地圖片選中上傳到頁面上,並且保存在資料庫,從資料庫讀取也可以顯示在頁面上

資料庫不能保存圖像,但是能保存圖像名,我給你個思路,用戶上傳後使用uid方法將圖片重命名,然後將新的名稱存在資料庫,圖片保存在一個已知路徑,然後頁面載入只需要將圖片src設置成"路徑/資料庫查詢結果。(建議通過id查詢)"

⑧ 用java寫上傳圖片時,怎麼實現預覽功能

圖片上傳後在網頁上直接讀取上傳後的地址進行預覽,就是說這個時候圖片已經上傳到伺服器了,如果要寫的嚴謹一些,可以在預覽後進行保存操作,如果不保存,則再寫一條語句把上傳上去的文件刪除

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

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

網路開源產品.

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

具體網址 :

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

熱點內容
准確的平特一肖演算法 發布:2025-02-04 16:50:50 瀏覽:73
lua調用python 發布:2025-02-04 16:49:57 瀏覽:348
jsp訪問不到資料庫資料庫資料庫 發布:2025-02-04 16:41:24 瀏覽:283
歐迪a4哪個配置最值得買 發布:2025-02-04 16:39:08 瀏覽:303
編譯包含調試信息的oat文件 發布:2025-02-04 16:39:07 瀏覽:152
一夢江湖官服怎麼在安卓下載 發布:2025-02-04 16:38:17 瀏覽:857
電腦華為雲空間哪裡密碼設置 發布:2025-02-04 16:22:15 瀏覽:756
sql不重復記錄 發布:2025-02-04 16:21:39 瀏覽:511
硬碟緩存速度 發布:2025-02-04 16:20:51 瀏覽:586
cmc的密碼是什麼意思 發布:2025-02-04 16:16:01 瀏覽:971