android's Introduction
android's People
Forkers
iversondeng zhibinjunhua allenymt cuijinquan xuxuefeng-0323 qgb1151521 namsangmi tianfengx99 cxp0542android's Issues
太复杂了
httpcore 在高版本安卓没反应,你这个可以,但是不能列出目录,目录默认返回 index.html,害我找半天!
代码太复杂了。
变量前加下划线?字符串设置为资源名称?
代码优化
1.自动获取文件类型:_contentType = URLConnection.getFileNameMap().getContentTypeFor(filePath);
2.Log.e(Thread.currentThread().getStackTrace()[2] + "", s); 可以打印行号。
3.显示目录功能:
public byte[] fetchContent() {
byte[] backData = null;
if (!isSupportMethod()) {
backData = fetchNotSupportMethodBack();
return backData;
}
String filePath = fetchFilePath();
Log.e(Thread.currentThread().getStackTrace()[2] + "", filePath);
File file = new File(filePath);
if (file.isDirectory()) {
backData = fetchFileList(_httpHeader.getFileName());
} else {
boolean hasFile = FileSp.isExist(filePath);
if (!hasFile) {
backData = fetchNotFoundBack();
return backData;
} else {
_contentType = URLConnection.getFileNameMap().getContentTypeFor(filePath);
backData = fetchBackFromFile(filePath);
}
}
return backData;
}
private byte[] fetchFileList(String dir) {
byte[] data = null;
File file = new File(Defaults.getRoot() + dir);
File[] files = file.listFiles();
Arrays.sort(files, new Comparator<File>() {
public int compare(File f1, File f2) {
long diff = f2.lastModified() - f1.lastModified();
if (diff > 0)
return 1;
else if (diff == 0)
return 0;
else
return -1;
}
});
String s = "<html>\n<head>\n<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>\n<title>文件服务</title>\n<style>\na { text-decoration:none; }\ntd { padding:0 20px; }\ntd:nth-child(2) { text-align:right; }\n</style>\n</head>\n<body>\n<h1>[" + dir + "]</h1>\n<table>\n<tr><th>名称</th><th>大小</th><th>创建时间</th></tr>\n";
for (int i = 0; i < files.length; i++) {
String target;
if (dir.equals("/")) {
target = dir;
} else {
target = dir + "/";
}
long ltime = files[i].lastModified();
Date date = new Date(ltime);
if (files[i].isDirectory()) {
s += "<tr><td><a href='" + target1 + files[i].getName() + "'>[" + files[i].getName() + "]</a></td><td></td><td>" + SDF.format(date) + "</td></tr>\n";
} else {
s += "<tr><td><a href='" + target1 + files[i].getName() + "'>" + files[i].getName() + "</a></td><td>" + files[i].length() / 1000 + " KB</td><td>" + SDF.format(date) + "</td></tr>\n";
}
}
s += "</table>\n</body>\n</html>";
try {
data = s.getBytes(_encoding);
} catch (UnsupportedEncodingException e) {
Log.e(Thread.currentThread().getStackTrace()[2] + "", e.toString());
}
return data;
}
4.中文转码
String fileName = "";
try {
fileName = URLDecoder.decode(_httpHeader.getFileName(), "UTF-8");
} catch (Exception e) {
Log.e(Thread.currentThread().getStackTrace()[2] + "", e.toString());
}
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.