當前位置:首頁 » 文件管理 » jfinal上傳文件路徑

jfinal上傳文件路徑

發布時間: 2023-07-21 15:59:12

1. JFinal如何實現圖片上傳

public void uploadpic(){
UploadFile upfile = getFile();//JFinal規定getFile()必須最先執行
File file = upfile.getFile();
String filename = file.getName();
String delfilename = filename;
if(filename!=null && !filename.equals("")){
filename = new SimpleDateFormat("yyyyMMddkkmmss").format(new Date())+filename;
/**
* 新保存的位置
*/
String path = getRequest().getSession().getServletContext()
.getRealPath("/");
String newPath = "/media/file/";//自定義目錄 用於存放圖片
/**
* 沒有則新建目錄
*/
File floder = new File(path + newPath);
if (!floder.exists()) {
floder.mkdirs();
}
/**
* 保存新文件
*/
FileInputStream fis = null;
FileOutputStream fos = null;
try{
File savePath = new File(path + newPath + filename);
if(!savePath.isDirectory()) savePath.createNewFile();
fis = new FileInputStream(file);
fos = new FileOutputStream(savePath);
byte[] bt = new byte[300];
while (fis.read(bt, 0, 300) != -1) {
fos.write(bt, 0, 300);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(null!=fis) fis.close();
if(null!=fos) fos.close();
}catch(Exception e){
e.printStackTrace();
}
}

/**
* 刪除原圖片,JFinal默認上傳文件路徑為 /upload(自動創建)
*/
File delFile = new File(path+"/upload/"+delfilename);
if(delFile.exists()){
delFile.delete();
}
setAttr("msg",filename);
setAttr("t",1);
}else{
setAttr("t",0);
}
renderJson();
}

2. jfinal如何實現文件上傳到非工程目錄下並可

1 , 設置全局

@Override

public void configConstant(Constants me) {

me.setBaseUploadPath(String baseUploadPath)

}

java">/**
*Setfilebaseuploadpath.
*設置文件上傳保存基礎路徑,當路徑以"/"打頭或是以windows磁碟盤符打頭,
*則將路徑設置為絕對路徑,否則路徑將是以應用根路徑為基礎的相對路徑
*<pre>
*例如:
*1:參數"/var/www/upload"為絕對路徑,上傳文件將保存到此路徑之下
*2:參數"upload"為相對路徑,上傳文件將保存到PathKit.getWebRoot()+"/upload"路徑之下
*</pre>
*/
publicvoidsetBaseUploadPath(StringbaseUploadPath){
if(StrKit.isBlank(baseUploadPath)){
("baseUploadPathcannotbeblank.");
}
this.baseUploadPath=baseUploadPath;
}


2 , 使用Java的文件拷貝或者移動
UploadFile.getFile().renameTo(newFile);

3. JFinal能夠批量上傳文件到ftp文件伺服器嗎

必須是可以的啊, 配合 jsch.jar(ftp文件上傳使用) 使用。

Controller 源碼中已經提供兩個獲取 批量上傳的文件 方法


publicList<UploadFile>getFiles(StringuploadPath,intmaxPostSize){
if(==false)
request=newMultipartRequest(request,uploadPath,maxPostSize);
return((MultipartRequest)request).getFiles();
}

publicList<UploadFile>getFiles(StringuploadPath){
if(==false)
request=newMultipartRequest(request,uploadPath);
return((MultipartRequest)request).getFiles();
}

獲取 到 List<UploadFile> 文件集合之後, 再使用 jsch.jar 進行ftp文件上傳到其他伺服器

JSchjsch=newJSch();
SessionsshSession=jsch.getSession(this.username,this.host,this.port);
sshSession.setPassword(password);
PropertiessshConfig=newProperties();
sshConfig.put("StrictHostKeyChecking","no");
sshSession.setConfig(sshConfig);
sshSession.connect(20000);
Channelsftp=sshSession.openChannel("sftp");
sftp.connect();
。。。這個網路搜一下,代碼很多,這里就不在啰嗦了
熱點內容
大話西遊手游怎麼看伺服器等級 發布:2025-03-15 20:21:53 瀏覽:220
rsa加密c源代碼 發布:2025-03-15 19:53:55 瀏覽:693
linux解壓bin 發布:2025-03-15 19:40:25 瀏覽:384
存儲數據為什麼只能使用兩種狀態 發布:2025-03-15 19:40:21 瀏覽:264
平方的運演算法則 發布:2025-03-15 19:38:57 瀏覽:971
江蘇省蘇州市社保卡初始登錄密碼是多少 發布:2025-03-15 19:38:55 瀏覽:516
安卓主板哪裡有賣 發布:2025-03-15 19:26:10 瀏覽:31
Q9源碼 發布:2025-03-15 19:24:21 瀏覽:177
芬蘭編程教育 發布:2025-03-15 18:59:46 瀏覽:427
網際網路的伺服器地址 發布:2025-03-15 18:53:01 瀏覽:893