📄 uploadfileserver.java
字号:
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.AccessController;
import java.security.MessageDigest;
import java.security.PrivilegedAction;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
@SuppressWarnings("unchecked")
public class UploadFileServer {
String realUserName = "ko";
String realPassword = "leo";
String uploadPath = "e:\\upload\\";
public boolean SaveFile(String username, String password, byte[] binData,
String fileName, int i, long fileLen) {
boolean success = false;
if (username.equals("") || username == null|| password == null)
return false;
success = username.equals(realUserName)
&& password.equals(realPassword);
if (!success)
return false;
if (binData == null || fileName.equals("") || fileName == null) {
return false;
}
String filePath = uploadPath + fileName;
File f = new File(filePath);
if (i == 0) {
if ((f.length() == fileLen)
&& calcMD5(fileToString(f)).equals(new String(binData))) {
unzipFile(f);
return true;
} else {
return false;
}
}
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
if (i == 1)
f.delete();
if (f.exists())
fos = new FileOutputStream(f, true);
else
fos = new FileOutputStream(f);
bos = new BufferedOutputStream(fos);
bos.write(binData);
bos.flush();
success = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bos != null) {
bos.close();
}
if (fos != null) {
fos.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return success;
}
boolean unzipFile(File f) {
boolean success = false;
try {
ZipFile zip = new ZipFile(f);
zip.close();
ZipInputStream in = new ZipInputStream(new FileInputStream(f));
ZipEntry file = in.getNextEntry();
int i = f.getAbsolutePath().lastIndexOf('.');
String dirname = new String();
if (i != -1)
dirname = f.getAbsolutePath().substring(0, i);
else
dirname = f.getAbsolutePath();
File newdir = new File(dirname);
newdir.mkdir();
byte[] c = new byte[1024];
int slen;
while (file != null) {
if (file.isDirectory()) {
File dirs = new File(file.getName());
dirs.mkdir();
dirs = null;
} else {
FileOutputStream out = new FileOutputStream(dirname
+ File.separator + file.getName());
while ((slen = in.read(c, 0, c.length)) != -1)
out.write(c, 0, slen);
out.close();
}
file = in.getNextEntry();
}
in.close();
success = true;
} catch (ZipException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return success;
}
FileChannel fc = null;
FileInputStream fis = null;
MappedByteBuffer mbb = null;
public ByteBuffer fileToString(File f) {
try {
fis = new FileInputStream(f);
fc = fis.getChannel();
mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
} catch (Exception e) {
e.printStackTrace();
}
return mbb;
}
public String calcMD5(ByteBuffer b) {
try {
MessageDigest alga = MessageDigest.getInstance("MD5");
alga.update(b);
byte[] digesta = alga.digest();
try {
if (fc != null)
fc.close();
if (fis != null)
fis.close();
if (mbb != null)
unmap(mbb);
} catch (Exception e) {
e.printStackTrace();
}
return byte2hex(digesta);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else
hs = hs + stmp;
if (n < b.length - 1)
hs = hs + "";
}
// return hs.toUpperCase();
return hs;
}
public void unmap(final Object buffer) throws Exception {
AccessController.doPrivileged(new PrivilegedAction<Object>() {
public Object run() {
try {
final Method getCleanerMethod = buffer.getClass()
.getMethod("cleaner", new Class[0]);
getCleanerMethod.setAccessible(true);
final sun.misc.Cleaner cleaner = (sun.misc.Cleaner) getCleanerMethod
.invoke(buffer, new Object[0]);
cleaner.clean();
} catch (final Exception e) {
e.printStackTrace();
}
return null;
}
});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -