当前位置:首页 » 文件管理 » 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-02-03 22:44:26 浏览:166
sql安装程序配置服务器失败 发布:2025-02-03 22:44:25 浏览:586
可以写脚本的点击器 发布:2025-02-03 22:44:22 浏览:612
c算法代码 发布:2025-02-03 22:42:20 浏览:862
脚本猫 发布:2025-02-03 22:36:30 浏览:137
qt软件如何反编译 发布:2025-02-03 22:36:27 浏览:22
linux串口数据接收 发布:2025-02-03 22:33:11 浏览:553
战双封脚本吗 发布:2025-02-03 22:32:14 浏览:446
可用内存存储 发布:2025-02-03 22:28:05 浏览:74
邮箱登录需要服务器地址是啥 发布:2025-02-03 22:27:27 浏览:550