㈠ java web 如何獲得文件上傳大小
有一種叫jspsmartupload的包用來簡化文件上傳下載的編寫
裡面可以獲取文件大小
//取得文件
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
//取得文件名
String fileName = myFile.getFileName();
//取得文件大小
int fileSize = myFile.getSize();
這是基於spring架構的上傳文件支持多個文件上傳,拿到file對象後,直接file.size()就可以獲取文件的大小,
if (request instanceof MultipartHttpServletRequest) {
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
for (Iterator it = multipartHttpServletRequest.getFileNames(); it.hasNext();) {
String key = (String) it.next();
MultipartFile file = multipartHttpServletRequest.getFile(key);
String originalFilename = file.getOriginalFilename();
long size = file.getSize();//文件大小需要轉換成KB或M
if (StringUtils.isNotBlank(originalFilename)) {
String suffixName = originalFilename.indexOf(".") == -1 ? "" : originalFilename.substring(originalFilename.lastIndexOf(".") + 1).toLowerCase();
try {
InputStream inputStream = file.getInputStream();
byte[] ToByteArray = FileCopyUtils.ToByteArray(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
㈡ 用java代碼如何查看本地一個文件的大小
publicstaticvoidgetFileSize(Stringpath){
//傳入文件路徑
Filefile=newFile(path);
//測試此文件是否存在
if(file.exists()){
//如果是文件夾
//這里只檢測了文件夾中第一層如果有需要可以繼續遞歸檢測
if(file.isDirectory()){
intsize=0;
for(Filezf:file.listFiles()){
if(zf.isDirectory())continue;
size+=zf.length();
}
System.out.println("文件夾"+file.getName()+"Size:"+(size/1024f)+"kb");
}else{
System.out.println(file.getName()+"Size:"+(file.length()/1024f)+"kb");
}
//如果文件不存在
}else{
System.out.println("此文件不存在");
}
}
㈢ 如何用java獲取網路文件的大小
try{
System.out.println("正在鏈接URL");
url=new URL("http://211.64.201.201/uploadfile/nyz.mp3");
HttpURLConnection urlcon=(HttpURLConnection)url.openConnection();
//根據響應獲取文件大小
fileLength=urlcon.getContentLength();
if(urlcon.getResponseCode()>=400){
System.out.println("伺服器響應錯誤");
System.exit(-1);
}
if(fileLength<=0)
System.out.println("無法獲知文件大小");
//列印信息
printMIME(urlcon);
System.out.println("文件大小為"+fileLength/1024+"K");
//獲取文件名
String trueurl=urlcon.getURL().toString();
String filename=trueurl.substring(trueurl.lastIndexOf('/')+1);
fileOut=new File("D://",filename);
}
catch(MalformedURLException e){
System.err.println(e);
}
catch(IOException e){
System.err.println(e);
}
㈣ android的java怎麼獲取文件大小
android中的java獲取文件大小的方法:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
public class FileContent {
private String path = "F:\\下載說明.txt";
public FileContent() throws IOException
{
File f = new File(path);
FileReader fileReader = new FileReader(f);
BufferedReader br = new BufferedReader(fileReader);
String str;
while((str = br.readLine() ) != null)
{
System.out.println(str);
}
System.out.println(new FileInputStream(new File(path)).available() / 1024 / 1024 +"M");
}
public static void main(String[] args) {
try {
new FileContent();
} catch (IOException e) {
e.printStackTrace();
}
}
}
㈤ JAVA讀取文件夾大小的幾種方法實例
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
public class Test {
public static void main(String[] args) throws Exception {
String searchStr = "APPTYPEID";
File searchDir = new File("D:\\xampp\\htdocs\\discuz");
List<Search> list = new ArrayList<Search>();
Collection<File> files = FileUtils.listFiles(searchDir, null, true);
List<String> lines = null;
for (File file : files) {
lines = FileUtils.readLines(file);
for (int i = 0; i < lines.size(); i++) {
if (lines.get(i).indexOf(searchStr) != -1) {
Search s = new Search();
s.setFilename(file.getName());
s.setLine(i + 1);
s.setContent(lines.get(i));
list.add(s);
}
}
}
StringBuffer sb = new StringBuffer();
for (Search s : list) {
sb.append(s.getFilename() + " , " + s.getLine() + " , " + s.getContent()).append(
IOUtils.LINE_SEPARATOR);
}
FileUtils.writeStringToFile(new File("D:\\search.txt"), sb.toString(), "UTF-8");
}
}
class Search {
private String filename;
private int line;
private String content;
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public void setLine(int line) {
this.line = line;
}
public int getLine() {
return line;
}
}
㈥ 利用Java文件類File的方法,獲取磁碟文件的文件名、長度、大小等特性。
package com.texst;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class GetFileMessageOfDisk {
/**
* @param args
*/
public static void main(String[] args) {
File file = new File("D:/");
getMessage(file);
}
private static void getMessage(File file){
File[] files = file.listFiles();
if(files==null){
files = new File[0];
}
for(int i=0;i<files.length;i++){
if(files[i].isDirectory()){
System.out.println(files[i].getPath());
getMessage(files[i]);
}else{
try {
int length = new FileInputStream(files[i]).available();
System.out.println(files[i].getName()+" 長度:"+length+" 大小:"+length/1024+"KB");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}