如何使用gwtupload从文件中获取文件内容
我正在使用GWTUpload上传一个文件。 我正在获取服务器信息、文件名、内容类型等。。在onfishHandler中,但没有从服务器到客户端获取文件内容的选项 注意:我正在尝试上载XML文件和EXCEL文件 我使用的是GWT2.4、GXT 3.0.1和GWTUpload 0.6.6 下面是示例代码 客户端代码-OnFinishHandler如何使用gwtupload从文件中获取文件内容,gwt,gwtupload,Gwt,Gwtupload,我正在使用GWTUpload上传一个文件。 我正在获取服务器信息、文件名、内容类型等。。在onfishHandler中,但没有从服务器到客户端获取文件内容的选项 注意:我正在尝试上载XML文件和EXCEL文件 我使用的是GWT2.4、GXT 3.0.1和GWTUpload 0.6.6 下面是示例代码 客户端代码-OnFinishHandler u.addOnFinishUploadHandler(new OnFinishUploaderHandler() { @Overr
u.addOnFinishUploadHandler(new OnFinishUploaderHandler() {
@Override
public void onFinish(IUploader uploader) {
if (uploader.getStatus() == Status.SUCCESS) {
System.err.println(uploader.getServerResponse());
UploadedInfo info = uploader.getServerInfo();
System.out.println("File name " + info.name);
System.out.println("File content-type " + info.ctype);
System.out.println("File size " + info.size);
System.out.println("Server message " + info.message);
}
}
});
Servlet代码
public class GWTFileUploadServlet extends UploadAction {
private static final long serialVersionUID = -6742854283091447922L;
String fileContent;
File uploadedFile;
@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 {
File file = File.createTempFile("upload-", ".bin");
item.write(file);
uploadedFile = file;
fileContent = item.getContentType();
response += "File saved as " + file.getAbsolutePath();
} catch (Exception e) {
throw new UploadActionException(e.getMessage());
}
}
}
removeSessionFileItems(request);
return response;
}
@Override
public void getUploadedFile(HttpServletRequest request,
HttpServletResponse response) throws IOException {
if (fileContent != null && !fileContent.isEmpty()) {
response.setContentType(fileContent);
FileInputStream is = new FileInputStream(uploadedFile);
copyFromInputStreamToOutputStream(is, response.getOutputStream());
} else {
renderXmlResponse(request, response, XML_ERROR_ITEM_NOT_FOUND);
}
}
}
公共类GWTFileUploadServlet扩展了UploadAction{
私有静态最终长serialVersionUID=-6742854283091447922L;
字符串文件内容;
文件上传文件;
@凌驾
公共字符串执行(HttpServletRequest,
列表会话文件)引发UploadActionException{
字符串响应=”;
int cont=0;
对于(文件项:会话文件){
if(false==item.isFormField()){
cont++;
试一试{
File File=File.createTempFile(“上传-”,“.bin”);
项目。写入(文件);
uploadedFile=文件;
fileContent=item.getContentType();
响应+=“文件另存为”+File.getAbsolutePath();
}捕获(例外e){
抛出新的UploadActionException(例如getMessage());
}
}
}
removeSessionFileItems(请求);
返回响应;
}
@凌驾
public void getUploadedFile(HttpServletRequest请求,
HttpServletResponse(响应)引发IOException{
if(fileContent!=null&&!fileContent.isEmpty()){
response.setContentType(fileContent);
FileInputStream is=新的FileInputStream(uploadedFile);
copyFromInputStreamToOutputStream(即response.getOutputStream());
}否则{
renderXmlResponse(请求、响应、XML错误、未找到项);
}
}
}
请帮助我……您可以在调用
item.write(…)
时读取您在文件系统中创建的文件,但最好从您收到的FileItem
中获取InputStream
,并将其内容写入任何位置。例如,如果内容是字符串
,则可以使用stringwriter
获取:
InputStream inputStream = item.getInputStream();
StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer);
String theContentString = writer.toString();
[已编辑]
要在客户端获取文件内容,您必须使用任何方法从服务器检索文件:
- 如果文件内容为ascii,则作为gwtupload servlet中的自定义消息:使用
的返回字符串executeAction
- 对servlet执行
调用,以使用uploader url值获取文件RequestBuilder
- 使用任何gwtajax机制,比如RPC
在现代浏览器中,您可以在客户端获取文件内容,而无需将其发送到服务器端。查看一下您可以直接使用的代码中的 字节[]文件; file=item.get()
您将以编码格式在“file”变量中获取所有文件内容。GwtUpload在内部使用IOUtils.copy将输入流复制到输出流。我的问题是如何获取正在复制到outputstream的文件内容。copyFromInputStreamToOutputStream(即response.getOutputStream());我想在onfinishhandler中得到响应我尝试使用gwtupload处理XML,我以自定义消息的形式获取文件内容。但是当我尝试上载excel文件时,它不工作,请帮助meExcel文件是二进制格式,因此您无法在自定义消息中返回其内容。您可以在服务器端使用Base64Utils将其转换为字符串,并使用我所说的任何方法返回到客户端,然后在客户端将其转换为二进制。我不明白的是,为什么你需要excel二进制文件在客户端,实际上你不能处理它的内容,至少在传统的方式,你必须使用html5的方法,这不是在所有浏览器中都可用。我的要求是,用户将上传一个文件,我必须读取内容,并将内容设置为dto中的字符串属性,然后将其发送到WebService。因此,首先需要在服务器端使用库来读取excel文件,您使用的是什么?。然后你必须提取你感兴趣的文本,你知道行和列吗?。并将其保存到数据库中。最后,您可以将此文本信息作为字符串发送回客户端。