❶ 在java中怎麼獲得圖片的路徑
你這是相對路徑,在項目文件夾下建一個image文件夾,然後那幾個對應的gif格式的圖片.也可以在那位置寫絕對路徑,D:/image/圖片名
❷ 怎樣將一個圖片文件夾打包成jar包打包後在java的程序想載入,文件路徑該怎麼寫
在復java中有jar命令 你可以在查看一制下jar幫助 很簡單的
用 getDocumentBase()定位到class文件的位置 在後面" "中添加你圖片的路徑加圖片名稱
例如:
Image Im=getImage(getDocumentBase(),"photo/1.jpg");
將photo文件夾和class放在同一級目錄就可以載入photo中的圖片(1.jpg)了
❸ 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圖片路徑問題
<img src="111.png" /> 引用的是同一目錄下的,你上面那段代碼根本就沒輸出到那個目錄當然找不到了。 對於一個項目來說圖片有個專有的文件夾存放,比如在WebRoot的imageFolder下,那麼你在輸出流寫圖片的時候要指定好這個相對路徑,然後頁面上用<img src="../imageFolder/111.png" />
通常來說解決方案是這樣的:以一個圖片上傳的例子為例,首先會在xml文件中配置好上傳文件的絕對路徑(例如C:/XXDX/XX這樣,將來部署到伺服器上時,修改配置文件路徑就好了)然後比如我們上傳的圖片都是用輸入流寫到xx/xx/war/upload中 (我看你後增加的代碼中沒有flush(),是不對的) 然後在頁面jsp中用/upload/xxx.png取值,之所以這么取是因為「/」是war根目錄,當然你要是用"../"那種方式你要查明白你現在的目錄級別。
❺ 在Java項目中上傳圖片時如何使上傳的圖片自動保存到指定路徑
用struts也可以實現 多文件上傳
下面是我寫的代碼,
參數中有要保存的目錄
作為參考!
/*文件目錄*/
public static String [] fileArray={
"logo.png",
"index.swf",
"OEMInfo.txt",
"favicon.ico"};
/**
* @author Caoshun
* @see 接收並保存文件
* */
public static void receiveAndSaveAllFileByPath(ActionForm form,String rootPath1,String rootPath2){
String fileName="";
//獲取表單中的文件資源
Hashtable<Object, Object> files = form.getMultipartRequestHandler().getFileElements();
//遍歷文件,並且循環保存
//當前處理文件序號
int file_num=1;
for (Enumeration<Object> e = files.keys(); e.hasMoreElements();) {
/*根據處理的當前文件下標,確定文件名*/
fileName=fileArray[file_num-1];
FormFile file = (FormFile) files.get((String) e.nextElement());
if (file != null && file.getFileSize() > 0) {
try {
//使用formfile.getInputStream()來獲取一個文件的輸入流進行保存。
//文件名
//String fileName = file.getFileName();
//System.out.println("debug in AddEnterpriceAction.java on line 152 fileName is : "+fileName);
//文件大小
//int fileSize = file.getFileSize();
//文件流
InputStream is = file.getInputStream();
//將輸入流保存到文件
//String rootPath = this.servlet.getServletContext().getRealPath("files");
//往cn中寫入
File rf = new File(rootPath1);
FileOutputStream fos = null;
fos = new FileOutputStream(new File(rf, fileName));
byte[] b = new byte[10240];
int real = 0;
real = is.read(b);
while (real > 0) {
fos.write(b, 0, real);
real = is.read(b);
}
//往en中寫入
File rf2 = new File(rootPath2);
InputStream is2 = file.getInputStream();
FileOutputStream fos2 = null;
fos2 = new FileOutputStream(new File(rf2, fileName));
byte[] b2 = new byte[10240];
int real2 = 0;
real2 = is2.read(b2);
while (real2 > 0) {
fos2.write(b2, 0, real2);
real2 = is2.read(b2);
}
//關閉文件流
fos.close();
is.close();
fos2.close();
is2.close();
} catch (RuntimeException e1) {
e1.printStackTrace();
} catch (Exception ee) {
ee.printStackTrace();
}
file.destroy();
}
file_num++;
}
}
❻ navicat 11.0漢化版 如何存入圖片路徑欄位類型應該怎麼設路徑應該怎麼寫Java代
圖片路徑就是一個字元串啊,你在後台查詢到圖片路徑,然後傳到前台圖片src="圖片路徑"不就可以了嗎?