jfinal上傳文件路徑
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();
。。。這個網路搜一下,代碼很多,這里就不在啰嗦了