GWT でファイルをアップロードしようとしていますが、これで 2 日間立ち往生しています。
今、私はいくつかのチュートリアルを試しているので、ここに私のコードがあります
私の以下のコードは決してサーバー側には行きません..
または、誰かが私にいくつかの実用的なコードを提供できる場合..
これは私のクライアント側のコードです
public class UploadDb extends Composite{
private FlowPanel panelImages = new FlowPanel();
public UploadDb() {
initWidget(panelImages);
// Create a new multiuploader and attach it to the document
MultiUploader defaultUploader = new MultiUploader(FileInputType.LABEL);
panelImages.add(defaultUploader);
defaultUploader.setEnabled(true);
// Add a finish handler which will load the image once the upload finishes
defaultUploader.addOnFinishUploadHandler(onFinishUploaderHandler);
}
// Load the image in the document and in the case of success attach it to the viewer
private IUploader.OnFinishUploaderHandler onFinishUploaderHandler = new IUploader.OnFinishUploaderHandler() {
public void onFinish(IUploader uploader) {
if (uploader.getStatus() == Status.SUCCESS) {
new PreloadedImage(uploader.fileUrl(), showImage);
System.out.println("Server message " + uploader.fileUrl());
}
}
};
// Attach an image to the pictures viewer
private OnLoadPreloadedImageHandler showImage = new OnLoadPreloadedImageHandler() {
public void onLoad(PreloadedImage image) {
image.setWidth("75px");
panelImages.add(image);
}
};
}
私のサーバー側で
public class DashBoardUploadServlet extends UploadAction {
private static final long serialVersionUID = 1L;
Hashtable<String, String> receivedContentTypes = new Hashtable<String, String>();
/**
* Maintain a list with received files and their content types.
*/
Hashtable<String, File> receivedFiles = new Hashtable<String, File>();
/**
* Override executeAction to save the received files in a custom place
* and delete this items from session.
*/
@Override
public String executeAction(HttpServletRequest request, List<FileItem> sessionFiles) throws UploadActionException {
String response = "";
int cont = 0;
for (FileItem item : sessionFiles) {
if (false == item.isFormField()) {
cont ++;
try {
/// Create a new file based on the remote file name in the client
// String saveName = item.getName().replaceAll("[\\\\/><\\|\\s\"'{}()\\[\\]]+", "_");
// File file =new File("/tmp/" + saveName);
/// Create a temporary file placed in /tmp (only works in unix)
// File file = File.createTempFile("upload-", ".bin", new File("/tmp"));
/// Create a temporary file placed in the default system temp folder
File file = File.createTempFile("upload-", ".bin");
item.write(file);
/// Save a list with the received files
receivedFiles.put(item.getFieldName(), file);
receivedContentTypes.put(item.getFieldName(), item.getContentType());
/// Send a customized message to the client.
response += "File saved as " + file.getAbsolutePath();
} catch (Exception e) {
throw new UploadActionException(e.getMessage());
}
}}
/// Remove files from session because we have a copy of them
removeSessionFileItems(request);
/// Send your customized message to the client.
return response;
}
web.xml
<servlet>
<servlet-name>UploadServlet</servlet-name>
<servlet-class>com.dashboard.server.DashBoardUploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UploadServlet</servlet-name>
<url-pattern>/Upload</url-pattern>
</servlet-mapping>