当前位置:首页 » 文件管理 » 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-07-05 16:10:30 浏览:515
微博数据库设计 发布:2025-07-05 15:30:55 浏览:19
linux485 发布:2025-07-05 14:38:28 浏览:299
php用的软件 发布:2025-07-05 14:06:22 浏览:750
没有权限访问计算机 发布:2025-07-05 13:29:11 浏览:425
javaweb开发教程视频教程 发布:2025-07-05 13:24:41 浏览:686
康师傅控流脚本破解 发布:2025-07-05 13:17:27 浏览:233
java的开发流程 发布:2025-07-05 12:45:11 浏览:678
怎么看内存卡配置 发布:2025-07-05 12:29:19 浏览:277
访问学者英文个人简历 发布:2025-07-05 12:29:17 浏览:828