當前位置:首頁 » 文件管理 » jsp上傳圖片代碼

jsp上傳圖片代碼

發布時間: 2025-09-07 01:38:17

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中上傳圖片不採用servlet,不採用任何框架,僅僅使用jsp代碼,保存到伺服器上。希望能得到指點。

那可能需要JS等一些的支持估計才可以了。首先你要考慮你這張圖片傳哪裡去,傳幾張。唯一性是否有。另外的話,你所說的什麼框架都不使用。只是jsp估計不行。因為jsp只是基於頁面的語言。編譯以後只是HTML了。所以可能還要在JSP上寫<%%>來套用java。最起碼你資料庫的連接是要寫的吧。連接好後,寫保存方法,是將哪些信息或欄位傳入DB中。並且是保存圖片路徑呢。還是把圖片二進制了保存起來。這些都是業務了,看你的個人需要。希望幫到你

㈢ java編程:怎麼用JSP(javabean)上傳一張圖片到伺服器的指定文件夾

先導smartupload jar包!在寫form表單<input tyle="file" enctype="multipart/form-data" method="post">enctype和method別寫錯了!
寫一個簡單的吧!
<%page import="com.jspsmart.upload.*"%>
<%
SmartUpload su=new SmartUpload ();//初始化SmartUpload對象
try{ //捕獲他可能出現的異常
su.upload();//執行上傳
}catch(Exception ex){
ex.printStackTrace;
}
File file=su.getFile().getFile(0); //(得到單個的上傳文件的信息)這里得到的File對象是你到的jar包里的com.jspsmart.upload.File類型 別寫成IO 裡面的File了
String filepath="upload\\"; //在這之前要在你所建項目的目錄下單建一個upload文件夾
filepath+=file.getFileName();
file.saveAs(filepath,SmartUpload.SAVE-VIRTUAL);
不知道是否建了與它相對應的資料庫表啊?
不懂得再玩吧!

%>

㈣ 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上傳文件的代碼 圖片 視頻 音頻WORD 之類的文件

這個比較簡單

選擇圖片的jsp頁面的form

<form action="doUploadImage.jsp" encType=multipart/form-data method=post >
本地選擇:
<input type="file" name="selPicture"
style="width: 330px; height: 23px; font-size: 16px">
<input type="submit" name="upload" id="upload" value="上傳"
style="width: 70px; height: 25px">
</form>
接收頁面

<%@ page language="java" import="java.util.*,com.jspsmart.upload.*,java.io.*"
pageEncoding="GBK"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>My JSP 'doUploadImage.jsp' starting page</title>

</head>

<body>
<%
request.setCharacterEncoding("GBK");
long size = 5 * 1024 * 1024;//允許上傳最大值為5MB
String fileType = "jpg,gif,JPG";//允許上傳文件類型
String imgName = null;//圖片名稱
byte[] data = null;//數據
String filePath = "";//文件路徑

//得到伺服器目錄webroot下的ImageFiles目錄的完整罩局路徑
String path = super.getServletContext().getRealPath("/Image");

System.out.println(path);

SmartUpload su = new SmartUpload();
//初始化
su.initialize(pageContext);
su.setMaxFileSize(size);
su.setAllowedFilesList(fileType);
su.setCharSet("GBK");
//上載文件
su.upload();
System.out.println(su.getSize());
su.getRequest();
//循環取得所有上載的文件
Files files = su.getFiles();
if (files != null) {
//如果文件路徑不存在則生成路徑
java.io.File fileDir = new java.io.File(path);
System.out.println("物穗讓存在");
if (!fileDir.exists()) {
fileDir.mkdirs();
System.out.println("不存在");
}

System.out.println(files.getCount());
//取出文件
for (int i = 0; i < files.getCount(); i++)
{
com.jspsmart.upload.File file = files.getFile(i);
if (file.isMissing()) continue;
if ("selPicture".equals(file.getFieldName())) {
String type = file.getFilePathName();
type = type.substring(type.lastIndexOf("."));
imgName = UUID.randomUUID().toString();//生成uuid作為圖片的名稱
imgName += type;
filePath = path + "/" + imgName;

//保存到指定文件
file.saveAs(filePath);

//讀取文件
data = readFile(filePath);
break;
}
}
}

if (data == null) {
out.print("族歲沒有圖片");
} else {
out.print("圖片上傳成功");
}
%>
<%!byte[] readFile(String filePath) {
ByteArrayOutputStream bos = null;
try {
FileInputStream fs = new FileInputStream(filePath);
bos = new ByteArrayOutputStream(5 * 1024 * 1024);
byte[] b = new byte[1024];
int len;
while ((len = fs.read(b)) != -1) {
bos.write(b, 0, len);
}
fs.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (bos == null) {
return null;
} else {
return bos.toByteArray();
}
}
%>
<%=request.getParameter("name") %>
</body>
</html>

有問題q我 379726806

後面data那一段時測試的 用的時候刪除掉 這是我寫的一個測試小工程 在項目裡面用的時候是把接收圖片放在servlet中的

我也是才搞了一個圖片上傳的東東

熱點內容
android圖片載入 發布:2025-09-07 06:10:48 瀏覽:492
mtp存儲器 發布:2025-09-07 05:58:00 瀏覽:948
php繼承類構造函數 發布:2025-09-07 05:57:07 瀏覽:703
指示資料庫 發布:2025-09-07 05:55:45 瀏覽:628
如何恢復伺服器系統 發布:2025-09-07 05:41:50 瀏覽:877
多個伺服器如何集中管理 發布:2025-09-07 05:37:00 瀏覽:416
python讀取文件行 發布:2025-09-07 05:34:14 瀏覽:662
mac版本ftp 發布:2025-09-07 05:27:09 瀏覽:531
條件編譯是不是預處理環節 發布:2025-09-07 05:21:21 瀏覽:639
java對集合的面試題 發布:2025-09-07 05:20:41 瀏覽:866