Salve a tutti
ho un problema che non riesco proprio a risolvere...
Questo è il mio codice:
codice:
class FileUploadProgressListener implements ProgressListener {
private Collection<ResumableGDataFileUploader> trackedUploaders = Lists.newArrayList();
private int pendingRequests;
PrintStream output;
Map<String, DocumentListEntry> uploaded = Maps.newHashMap();
Map<String, String> failed = Maps.newHashMap();
boolean processed;
public FileUploadProgressListener()
{
this.pendingRequests = 0;
}
public void listenTo(Collection<ResumableGDataFileUploader> uploaders)
{
this.trackedUploaders.addAll(uploaders);
this.pendingRequests = trackedUploaders.size();
}
public synchronized void progressChanged(ResumableHttpFileUploader uploader)
{
String fileId = ((FileUploadData) uploader.getData()).getFileName();
switch(uploader.getUploadState()) {
case COMPLETE:
case CLIENT_ERROR:
pendingRequests -= 1;
output.println(fileId + ": Completed");
break;
case IN_PROGRESS:
output.println(fileId + ":"
+ String.format("%3.0f", uploader.getProgress() * 100) + "%");
break;
case NOT_STARTED:
output.println(fileId + ":" + "Not Started");
break;
}
}
public synchronized boolean isDone() {
// not done if there are any pending requests.
if (pendingRequests > 0) {
return false;
}
// if all responses are processed., nothing to do.
if (processed) {
return true;
}
// check if all response streams are available.
for (ResumableGDataFileUploader uploader : trackedUploaders) {
if (!uploader.isDone()) {
return false;
}
}
// process all responses
for (ResumableGDataFileUploader uploader : trackedUploaders) {
String fileId = ((FileUploadData) uploader.getData()).getFileName();
switch(uploader.getUploadState()) {
case COMPLETE:
try {
DocumentListEntry entry =
uploader.getResponse(DocumentListEntry.class);
uploaded.put(fileId, entry);
} catch (IOException e) {
failed.put(fileId, "Upload completed, but unexpected error "
+ "reading server response");
} catch (ServiceException e) {
failed.put(fileId,
"Upload completed, but failed to parse server response");
}
break;
case CLIENT_ERROR:
failed.put(fileId, "Failed at " + uploader.getProgress());
break;
}
}
processed = true;
output.println("All requests done");
return true;
}
public synchronized Collection<DocumentListEntry> getUploaded() {
if (!isDone()) {
return null;
}
return uploaded.values();
}
public synchronized void printResults() {
if (!isDone()) {
return;
}
output.println("Result: " + uploaded.size() + ", " + failed.size());
if (uploaded.size() > 0) {
output.println(" Successfully Uploaded:");
for (Map.Entry<String, DocumentListEntry> entry : uploaded.entrySet()) {
printDocumentEntry(entry.getValue());
}
}
if (failed.size() > 0) {
output.println(" Failed to upload:");
for (Map.Entry entry : failed.entrySet()) {
output.println(" " + entry.getKey() + ":" + entry.getValue());
}
}
}
/**
* Prints out the specified document entry.
*
* @param doc the document entry to print.
*/
public void printDocumentEntry(DocumentListEntry doc) {
StringBuffer buffer = new StringBuffer();
buffer.append(" -- " + doc.getTitle().getPlainText() + " ");
if (!doc.getParentLinks().isEmpty()) {
for (Link link : doc.getParentLinks()) {
buffer.append("[" + link.getTitle() + "] ");
}
}
buffer.append(doc.getResourceId());
output.println(buffer);
}
} // CHIUSURA CLASSE
try
{
DocsService client = new DocsService("Google Docs API");
client.setUserCredentials(usr, psw);
String percorsoFile = request.getParameter("file");
int MAX_CONCURRENT_UPLOADS = 10;
int PROGRESS_UPDATE_INTERVAL = 1000;
int DEFAULT_CHUNK_SIZE = 10485760;
File file = new File(percorsoFile);
FileUploadProgressListener listener = new FileUploadProgressListener();
ExecutorService executor = Executors.newFixedThreadPool(MAX_CONCURRENT_UPLOADS);
List<ResumableGDataFileUploader> uploaders = Lists.newArrayList();
String contentType = DocumentListEntry.MediaType.fromFileName(file.getName()).getMimeType();
MediaFileSource mediaFile = new MediaFileSource(file, contentType);
URL createUploadUrl = new URL("https://docs.google.com/feeds/upload/create-session/default/private/full");
ResumableGDataFileUploader uploader = new ResumableGDataFileUploader.Builder(client, createUploadUrl, mediaFile, null)
.chunkSize(DEFAULT_CHUNK_SIZE).executor(executor)
.title(mediaFile.getName())
.trackProgress(listener, PROGRESS_UPDATE_INTERVAL)
.build();
uploaders.add(uploader);
listener.listenTo(uploaders);
for (ResumableGDataFileUploader uploader1 : uploaders)
{
uploader1.start();
}
while (!listener.isDone())
{
try
{
Thread.sleep(100);
} catch (InterruptedException ie)
{
System.out.println("Interrotto!");
listener.printResults();
throw ie; // rethrow
}
}
listener.printResults();
}catch (Exception ex)
{
System.err.println("Eccezzione " + ex.getMessage());
ex.printStackTrace();
}
Il problema che mi da è:
codice:
Exception in thread "Timer-0" java.lang.NullPointerException
at org.apache.jsp.upload_jsp$1FileUploadProgressListener.progressChanged(upload_jsp.java:138)
at com.google.gdata.client.uploader.ResumableHttpFileUploader$NotificationTask.run(ResumableHttpFileUploader.java:179)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)
Qualcuno può aiutarmi???
Grazie a tutti