我正在使用JSch从SFTP服务器获取文件,但我试图想办法只获取最旧的文件,并确保它当前没有被写入.我想象自己这样做的方法是首先找到指定的远程文件夹中的哪个文件是最旧的.然后我会检查文件大小,等待x秒(可能大约10秒,只是为了安全),然后再次检查.如果文件大小没有改变,我下载文件并进行处理.但是,我不知道该怎么做!如果有人知道如何做到这一点,或者知道其他支持具有此内置功能的SFTP(我知道Apache Commons,但只有FTPS),我们将不胜感激.
提前致谢.
下面的方法返回文件大小2.由于它很长,我假设java计算的文件大小是2*64位.但实际上我保存了32位int + 16位char = 48位.为什么Java会进行这种转换?此外,无论是char还是int,Java是否隐式存储文件中的所有内容?如何获得48位的准确大小?
public static void main(String[] args)
{
File f = new File("C:/sam.txt");
int a= 42;
char c= '.';
try {
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
PrintWriter pw = new PrintWriter(f);
pw.write(a);
pw.write(c);
pw.close();
System.out.println("file size:"+f.length());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud) 如何使用命令检查新文件的大小是否明显小于它要替换的文件的大小?
我每晚都有一个批处理文件,除其他外,它调用一个应用程序UpdVMem.exe,该应用程序VMembers.Adt从主文件创建一个减少的成员数据库Members.Adt.然后将此文件移动到具有相同脚本的远程站点.
在一些站点上,VMembers.Adt会定期被破坏.我不知道为什么,因为我们已经排除了表格被锁定进行编辑(通过我的Delphi Membership软件).它通常看似相同的大小,但包含不到一半的记录.
更好的是一组可以检测到这种损坏或执行失败的命令UpdVMem.exe,因为单独的大小不是最好的指标.
谢谢
我正在尝试编写一个简单的perl脚本,它将遍历目录中的常规文件并计算放在一起的所有文件的总大小.但是,我无法获得文件的实际大小,我无法弄清楚原因.这是代码的相关部分.我输入print语句进行调试:
$totalsize = 0;
while ($_ = readdir (DH)) {
print "current file is: $_\t";
$cursize = -s $_;
print "size is: $cursize\n";
$totalsize += $cursize;
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的输出:
current file is: test.pl size is:
current file is: prob12.pl size is:
current file is: prob13.pl size is:
current file is: prob14.pl size is:
current file is: prob15.pl size is:
Run Code Online (Sandbox Code Playgroud)
因此文件大小保持空白.我尝试使用,$cursize = $_但唯一的影响是检索当前和父目录的文件大小,每个4096字节; 它仍然没有获得常规文件的任何实际文件大小.
我已经在网上查看了我在perl上的几本书,似乎perl无法获取文件大小,因为脚本无法读取文件.我通过输入if语句测试了这个:
print "Cannot read file $_\n" if (! -r _);
Run Code Online (Sandbox Code Playgroud)
果然每个文件都有错误,说明文件无法读取.我不明白为什么会这样.有问题文件的目录是我的主目录的子目录,我从我的主目录中的另一个子目录中运行脚本.我已阅读所有相关文件的权限.我尝试将文件模式更改为755(从之前的711),但我仍然得到Cannot read file每个文件的输出.
我不明白发生了什么.我可能会混淆运行perl脚本时权限如何工作,或者我对正确的使用方式感到困惑 …
在记事本中打开一个新文件并在其中插入没有引号的句子,"四分和七年前".
Four 4 characters
score 5 characters
and 3 characters
seven 5 characters
years 5 characters
ago 3 characters
Run Code Online (Sandbox Code Playgroud)
总计:25 + 5空格= 30个字符.
您会发现该文件在磁盘上的大小为30个字节:每个字符1个字节.以getSize.txt名称将文件保存到磁盘.然后查看文件的大小.通常,每个字符都占用一个字节.
大小:30字节
磁盘大小:4.00 KB(4,096字节)
以下段落是从pdf复制粘贴的.
如果您在计算机查看时查看该文件,您会发现每个字节不包含字母而是数字 - 该数字是与该字符对应的ASCII代码(见下文).所以在磁盘上,文件的数字如下所示:
F ourandseven
70 111 117 114 32 97 110 100 32 115 101 118 101 110
通过查看ASCII表,您可以看到每个字符与使用的ASCII代码之间的一一对应关系.注意32用于空格--32是空格的ASCII码.如果我们想要技术上正确的话,我们可以将这些十进制数扩展为二进制数(所以32 = 00100000) - 这就是计算机真正处理事物的方式.
1)我知道每件事都以比特和字节的形式存储,所以通常这意味着 - "你会发现每个字节不包含字母而是数字 - 数字是与字符对应的ASCII码" .一个字节是8位.那么"每个字节一个数字 - 数字是ASCII代码"怎么样?除了0和1之外,一个字节如何包含一个ASCII数字(例如,49为'1')?
2)磁盘上的大小和大小有什么区别?ASCII和Unicode如何适应它?
3)在Java中,字符串是对象.我可以说这是一个多个字符连在一起吗?String str ="四分和七年前"那么str如何存储在内存中.它与保存在记事本文件中的方式相同吗?
请在这里查看我的代码https://github.com/steelx/ReactBasic
运行gulp
并转到./public/文件夹后,您可以看到main.js其文件大小为1.8MB
我需要明白,为什么它如此巨大.
我正在编写一个需要根据文件大小(以字节为单位)对文件执行操作的函数。我想尽量减少传递给函数的参数数量,所以我只会将句柄传递给已经打开的文件,并让函数获取大小。有没有一种优雅的方法来做到这一点?
我正在尝试以下操作,os.path.getsize(os.path.abspath(file_id))但它不起作用:
def datafile_profiler(file_id):
filesize = os.path.getsize(os.path.abspath(file_id))
#[...] continue doing things with the file, based on the size in bites
return stuff
Run Code Online (Sandbox Code Playgroud)
然后,从“主代码”
file_id = open(filepath, "rb")
stuff = datafile_profiler(file_id)
file_id.close()
Run Code Online (Sandbox Code Playgroud)
欢迎任何建议(也是一种完全不同的方法)。谢谢。
我正在将文件(任何格式的 doc、docx、pdf、文本等)作为多部分表单/数据从 Postman 或应用程序 UI 上传到 REST API。文本文件上传正常。所有其他非文本格式都会损坏。我打不开那些文件。
上传文件的大小急剧增加。检查以下服务器日志:
File size just before call to request.getRequestDispatcher().forward(): 27583
Controller, first line in method:39439
Run Code Online (Sandbox Code Playgroud)
上传文件大小为27.3Kb
我猜这些文件因为附加到文件中的其他数据而损坏。
控制器方法是
@RequestMapping(value="/entity/{entity}/{entityId}/type/{type}/{derive}",method = RequestMethod.POST)
@ResponseBody
public String uploadFile(@RequestParam("file") MultipartFile multipartFile,@PathVariable("entity")String entity,@PathVariable("type")String type,@PathVariable("entityId")Long entityId,@PathVariable("derive") boolean derive) throws Exception
Run Code Online (Sandbox Code Playgroud)
由于文本文件正确保存并且其他文件也被正确写入,不要认为写入文件的代码不正确。
获取 inputStream 的代码
public String storeFile(MultipartFile multipartFile, String entity, Long id, String uploadType, boolean isDerive,String customName)
throws Exception
{
try
{
System.out.println(multipartFile.getSize());
String fileName = "";
String contentType = "";
if (multipartFile != null)
{
fileName = multipartFile.getOriginalFilename(); …Run Code Online (Sandbox Code Playgroud) 由于某种原因,我的 git 历史记录中有 2 个非常大的文件,我需要删除它们,因为当我尝试推送时它们超过了 git 的文件大小。我已经从本地的整个 git 历史记录中删除了它们,然后重新推送。但是,当我尝试推送到 heroku 时,它说我在本地缺少工作,我需要拉。如果我运行git pull heroku master,它会再次拉下这 2 个大文件,阻止我再次将最新更改推送到 heroku。
是否有可能以某种方式从我的 Heroku 应用程序中删除这两个文件?它们只是图像文件。
import paramiko
from socket import error as socket_error
import os
server =['10.10.0.1','10.10.0.2']
path='/home/test/'
for hostname in server:
try:
ssh_remote =paramiko.SSHClient()
ssh_remote.set_missing_host_key_policy(paramiko.AutoAddPolicy())
privatekeyfile = os.path.expanduser('~/.ssh/id')
mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile, password='test123')
ssh_remote.connect(hostname, username = 'test1', pkey = mykey)
sftp=ssh_remote.open_sftp()
for i in sftp.listdir(path):
info = sftp.stat(i)
print info.st_size
except paramiko.SSHException as sshException:
print "Unable to establish SSH connection:{0}".format(hostname)
except socket_error as socket_err:
print "Unable to connect connection refused"
Run Code Online (Sandbox Code Playgroud)
这是我的代码。我试图获取远程服务器文件的文件大小。但是下面的错误正在抛出。可以请一些指导吗?
错误
import paramiko
from socket import error as socket_error
import os
server =['10.10.0.1','10.10.0.2']
path='/home/test/' …Run Code Online (Sandbox Code Playgroud) filesize ×10
java ×4
python ×2
sftp ×2
batch-file ×1
bit ×1
delphi ×1
file ×1
file-io ×1
file-upload ×1
git ×1
gulp ×1
heroku ×1
jsch ×1
paramiko ×1
perl ×1
permissions ×1
printwriter ×1
python-2.7 ×1
reactjs ×1
string ×1