当前位置:首页 » 文件管理 » 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 21:45:01 浏览:694
查询网站服务器mac地址 发布:2025-03-15 21:45:00 浏览:40
奇骏原厂配置有哪些 发布:2025-03-15 21:28:13 浏览:389
乐视投屏安卓怎么全屏 发布:2025-03-15 21:27:33 浏览:476
闲置电脑打造影音服务器 发布:2025-03-15 21:17:26 浏览:415
知网查数据库吗 发布:2025-03-15 21:11:44 浏览:296
c语言截屏 发布:2025-03-15 21:06:57 浏览:608
安卓手机桌宠哪个最好 发布:2025-03-15 21:05:08 浏览:465
vps自动脚本 发布:2025-03-15 20:50:29 浏览:60
php刷新重复提交 发布:2025-03-15 20:50:26 浏览:307