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

java中圖片上傳

發布時間:2024-03-10 10:54:05

java上傳圖片到伺服器指定路徑

privateFilemyFile;//文件
;//類型
privateStringmyFileFileName;//文件名
//。。。。getXXX()setXXX()方法

//輸入流
InputStreamis=newFileInputStream(myFile);
//設定文件路徑
StringphotoPath=ServletActionContext.getServletContext()
.getRealPath("/user/photo/");
FilefilePhotoPath=newFile(photoPath);
//判斷這個路徑是否存在,如果不存在創建這個路徑
if(!filePhotoPath.isDirectory()){
filePhotoPath.mkdir();
}

Stringextension=FilenameUtils.getExtension(this
.getMyFileFileName());//後綴名比如jpg
Stringfilename=UUID.randomUUID().toString()+"."+extension;

//目標文件
Filetofile=newFile(photoPath,filename);
//輸出流
OutputStreamos=newFileOutputStream(tofile);
byte[]buffer=newbyte[1024];
intlength=0;
while((length=is.read(buffer))>0){
os.write(buffer,0,length);
}
//關閉輸入流
is.close();
//關閉輸出流
os.close();

㈡ java實現圖片上傳至伺服器並顯示,如何做希望要具體的代碼實現

很簡單。
可以手寫IO讀寫(有點麻煩)。
怕麻煩的話使用FileUpload組件 在servlet里doPost嵌入一下代碼
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html;charset=gb2312");
PrintWriter out=response.getWriter();

//設置保存上傳文件的目錄
String uploadDir =getServletContext().getRealPath("/up");
System.out.println(uploadDir);
if (uploadDir == null)
{
out.println("無法訪問存儲目錄!");
return;
}
//根據路徑創建一個文件
File fUploadDir = new File(uploadDir);
if(!fUploadDir.exists()){
if(!fUploadDir.mkdir())//如果UP目錄不存在 創建一個 不能創建輸出...
{
out.println("無法創建存儲目錄!");
return;
}
}

if (!DiskFileUpload.isMultipartContent(request))
{
out.println("只能處理multipart/form-data類型的數據!");
return ;
}

DiskFileUpload fu = new DiskFileUpload();
//最多上傳200M數據
fu.setSizeMax(1024 * 1024 * 200);
//超過1M的欄位數據採用臨時文件緩存
fu.setSizeThreshold(1024 * 1024);
//採用默認的臨時文件存儲位置
//fu.setRepositoryPath(...);
//設置上傳的普通欄位的名稱和文件欄位的文件名所採用的字元集編碼
fu.setHeaderEncoding("gb2312");

//得到所有表單欄位對象的集合
List fileItems = null;
try
{
fileItems = fu.parseRequest(request);//解析request對象中上傳的文件

}
catch (FileUploadException e)
{
out.println("解析數據時出現如下問題:");
e.printStackTrace(out);
return;
}

//處理每個表單欄位
Iterator i = fileItems.iterator();
while (i.hasNext())
{
FileItem fi = (FileItem) i.next();
if (fi.isFormField()){
String content = fi.getString("GB2312");
String fieldName = fi.getFieldName();
request.setAttribute(fieldName,content);
}else{
try
{
String pathSrc = fi.getName();
if(pathSrc.trim().equals("")){
continue;
}
int start = pathSrc.lastIndexOf('\\');
String fileName = pathSrc.substring(start + 1);
File pathDest = new File(uploadDir, fileName);

fi.write(pathDest);
String fieldName = fi.getFieldName();
request.setAttribute(fieldName, fileName);
}catch (Exception e){
out.println("存儲文件時出現如下問題:");
e.printStackTrace(out);
return;
}
finally //總是立即刪除保存表單欄位內容的臨時文件
{
fi.delete();
}

}
}
注意 JSP頁面的form要加enctype="multipart/form-data" 屬性, 提交的時候要向伺服器說明一下 此頁麵包含文件。

如果 還是麻煩,乾脆使用Struts 的上傳組件 他對FileUpload又做了封裝,使用起來更傻瓜化,很容易掌握。

-----------------------------
以上回答,如有不明白可以聯系我。

㈢ java 中如何向伺服器上傳圖片

我們使用一些已有的組件幫助我們實現這種上傳功能。
常用的上傳組件:
Apache 的 Commons FileUpload
JavaZoom的UploadBean
jspSmartUpload
以下,以FileUpload為例講解
1、在jsp端
<form id="form1" name="form1" method="post" action="servlet/fileServlet" enctype="multipart/form-data">
要注意enctype="multipart/form-data"
然後只需要放置一個file控制項,並執行submit操作即可
<input name="file" type="file" size="20" >
<input type="submit" name="submit" value="提交" >
2、web端
核心代碼如下:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List items = upload.parseRequest(request);
Iterator itr = items.iterator();
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();
if (item.isFormField()) {
System.out.println("表單參數名:" + item.getFieldName() + ",表單參數值:" + item.getString("UTF-8"));
} else {
if (item.getName() != null && !item.getName().equals("")) {
System.out.println("上傳文件的大小:" + item.getSize());
System.out.println("上傳文件的類型:" + item.getContentType());
System.out.println("上傳文件的名稱:" + item.getName());
File tempFile = new File(item.getName());
File file = new File(sc.getRealPath("/") + savePath, tempFile.getName());
item.write(file);
request.setAttribute("upload.message", "上傳文件成功!");
}else{
request.setAttribute("upload.message", "沒有選擇上傳文件!");
}
}
}
}catch(FileUploadException e){
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
request.setAttribute("upload.message", "上傳文件失敗!");
}
request.getRequestDispatcher("/uploadResult.jsp").forward(request, response);
}

㈣ java 圖片上傳,圖片上傳是上傳到哪裡比較好伺服器上還是資料庫

上傳到伺服器上會比較好,資料庫可以記錄一些必要的信息,比如文件的名稱,類型(擴展名),大小,創建時間及誰的文件等等;如果把文件放入資料庫,讀寫性能不如文件系統,還會加大資料庫的壓力。放到伺服器上注意要設計好一個文件結構,不然以後一個文件夾里有成千上萬個文件,打開那個文件夾就要好久,更不用說查找和下載了。

閱讀全文

與java中圖片上傳相關的資料

熱點內容
葉子java版命令方塊 瀏覽:837
阿里伺服器有什麼作用 瀏覽:749
java程序開發範例 瀏覽:888
java17formac下載 瀏覽:614
rrc是什麼單片機 瀏覽:530
linux默認網關設置 瀏覽:679
java函數return 瀏覽:25
鄭州app積分商城哪裡好 瀏覽:610
7za命令解壓zip 瀏覽:906
硬碟加密無法進入系統 瀏覽:560
四葉花演算法 瀏覽:741
寶寶濾鏡app哪裡下載 瀏覽:1002
java保護代碼 瀏覽:806
游戲海外伺服器什麼意思 瀏覽:568
快手網紅程序員排名 瀏覽:99
首先會通過什麼尋找伺服器的ip地址 瀏覽:199
網頁代碼加密解碼 瀏覽:285
wyks1ms文件夾 瀏覽:93
什麼app可以看柯南高清 瀏覽:425
加密的盤文件恢復 瀏覽:22