下面是我写的一个程序, 主要是从数据库中查找要上传的文件, 然后上传到服务器,
感觉写的有一点乱,谁提一点意见 Fetcher producer = new Fetcher();
Uploader uploader = new Uploader();
executor.submit(producer);
executor.submit(uploader);public class UploadAction implements ApplicationContextAware, Lifecycle {
final private static Logger LOGGER = Logger.getLogger(UploadAction.class.getName()); private ExecutorService executor; /** how long after crawl start to first scan action directory */
protected int initialDelaySeconds = 10; public int getInitialDelaySeconds() {
return initialDelaySeconds;
} public void setInitialDelaySeconds(int initialDelay) {
this.initialDelaySeconds = initialDelay;
} /** delay between scans of actionDirectory for new files */
protected int delaySeconds = 20; public int getDelaySeconds() {
return delaySeconds;
} public void setDelaySeconds(int delay) {
this.delaySeconds = delay;
} ApplicationContext appCtx; public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.appCtx = applicationContext;
} public boolean isRunning() {
return executor != null && !executor.isShutdown();
} private String prefix; public String getPrefix() {
return prefix;
} public void setPrefix(String prefix) {
this.prefix = prefix;
} @Autowired
private FtpUtil ftpUtil;

@Autowired
private DownloadFileService downloadFileService; private BlockingQueue<DownloadFile> queue = new ArrayBlockingQueue<DownloadFile>(10); public void start() {
if (isRunning()) {
return;
} // start background executor
executor = Executors.newCachedThreadPool(); Fetcher producer = new Fetcher();
Uploader uploader = new Uploader();
executor.submit(producer);
executor.submit(uploader);
} public void stop() {

executor.shutdown();
try {
while (!executor.awaitTermination(10, TimeUnit.SECONDS))
;
} catch (InterruptedException e) {
// do nothing
}
} private class Fetcher implements Runnable { @Override
public void run() { if (!queue.isEmpty()) {
return;
} List<DownloadFile> downloadFileList = downloadFileService.getNotUploadList(); if (downloadFileList != null && downloadFileList.size() > 0) { try {
for (int i = 0; i < downloadFileList.size(); i++) {
DownloadFile downloadFile = downloadFileList.get(i);
queue.put(downloadFile);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} } private class Uploader implements Runnable { @Override
public void run() { try {
while (true) {
try {
ftpUtil.connectServer();
DownloadFile downloadFile = queue.take();
String saveUrl = downloadFile.getSaveUrl(); Thread.currentThread().setName("com.spider.util.UploadAction:" + saveUrl);
String fileName = URLDecoder.decode(saveUrl.substring(saveUrl.lastIndexOf("/") + 1), "UTF-8");
String fileDir = saveUrl.substring(1, saveUrl.lastIndexOf("/")); boolean success = ftpUtil.upload(fileDir, fileName, new FileInputStream(new File(prefix + File.separator
+ fileDir + File.separator + fileName))); if (success) {
downloadFile.setIsUpload(true);
downloadFileService.update(downloadFile);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (SocketTimeoutException e) {
LOGGER.log(Level.SEVERE, "Failed upload the url to server " + Thread.currentThread().getName(), e);
} catch (FileNotFoundException e) {
LOGGER.log(Level.SEVERE, "Failed upload the url to server " + Thread.currentThread().getName(), e);
} catch (IOException e) {
e.printStackTrace();
} finally {
ftpUtil.closeConnect();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
} }
}