package com.jqmobile.core.server.servlet.load;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import com.jqmobile.core.server.Application;
public class UploadClient {
public static void main(String[] args) throws Exception {
UploadClient uc = UploadClient.openUpload("d:/upload");
String name = "老猫-哥已经爱上你啦";
up(uc, name);
down(uc, name);
name = "Sara-我的心好冷(MTV)-国语-853523";
up(uc, name);
down(uc, name);
}
private static void down(UploadClient uc, String name) throws Exception {
InputStream i = uc.download(name+".mpg");
// i.available();
File f = new File("d:/upload/"+name+"——"+System.currentTimeMillis()+".mpg");
FileOutputStream o = new FileOutputStream(f);
byte[] bs = new byte[i.available()];
int r = i.read(bs);
o.write(bs);
while(-1 != r){
r = i.read();
o.write(r);
}
o.flush();
o.close();
}
static void up(UploadClient uc, String name) throws FileNotFoundException {
String s = name+".mpg";
FileInputStream in = new FileInputStream(new File("d:/" + s));
uc.upload(s, in);
}
//
//private final static UploadClient defaultUpload = openUpload("G:\\myeclse\\workspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\patrol\\themes");
private final static UploadClient defaultUpload = openUpload("/home/zjxft/patrol/themes/clientUpload");
public static UploadClient openUpload(){
return defaultUpload;
}
public static UploadClient openUpload(String uri){
return new UploadClient(uri);
}
private final String url;
private UploadClient(String url){
this.url = url;
}
public boolean upload(String filename, InputStream in){
LoadListen listen = Application.getLoadListen();
if(null != listen){
try {
if(listen.upload(filename, in))
return true;
} catch (Throwable e) {
// e.printStackTrace();
return false;
}
}
File file = getFile();
File f = new File(file, filename);
if(!f.exists()){
try {
f.createNewFile();
FileOutputStream fw = new FileOutputStream(f);
// byte[] bs = new byte[1024];
byte[] bs = new byte[in.available()];
if(in.read(bs) != -1){
fw.write(bs);
}
int b;
while ((b=in.read())!=-1) {
fw.write(b);
}
fw.flush();
fw.close();
return true;
} catch (IOException e) {
System.out.println("file url:================"+f.getPath());
e.printStackTrace();
return false;
}
}
return false;
}
private File directory;
private File getFile() {
if(null == directory){
if(null == url){
URL r = Thread.currentThread().getContextClassLoader().getResource("");
try {
r = new URL(r, "load");
File f = new File(r.toURI());
if(!f.exists()){
f.mkdirs();
}
directory = f;
} catch (Exception e) {
e.printStackTrace();
}
}else{
File f = new File(url);
if(!f.exists()){
f.mkdirs();
}
directory = f;
}
}
return directory;
}
public InputStream download(String filename){
LoadListen listen = Application.getLoadListen();
if(null != listen){
try {
InputStream download = listen.download(filename);
if(null != download){
return download;
}
} catch (Throwable e) {
e.printStackTrace();
}
}
File file = getFile();
File f = new File(file, filename);
if(f.exists()){
try {
return new FileInputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
return null;
}
}