上传文件到Glassfish备用文档根文件夹
Uploading file to Glassfish alternate document root folder
我在glassfish-web.xml
中为Glassfish创建了一个备用文档根:
<glassfish-web-app>
<property name="alternatedocroot_1" value="from=/Database/* dir=c:/GetSome" />
</glassfish-web-app>
JS上传文件,如果成功,则加载jsp和上传的文件:
function Changed_inputFileLogo()
{
/*console.log("function Changed_inputFileLogo");*/
current = event.target;
var data = new FormData();
$.each(current.files, function(key, value)
{
data.append(key, value);
});
data.append("idOrganization", _Organization.id);
data.append("requestPage", true);
$.ajax({
type: 'POST',
async: false,
url: 'UploadLogo',
data: data,
processData: false,
contentType: false,
success: function(response)
{
_Organization.logo = 1;
$.ajax({
type: 'POST',
async: false,
url: 'PhotoExist.jsp',
data: "path=" + "Database/LogoOrganization/"+_Organization.id+".jpg",
success: function(response)
{
$("#divLogo").html(response);
},
error: function(xhr, textStatus, errorThrown)
{
LogConsole_ajaxError(xhr, textStatus, errorThrown);
}
});
},
error: function(xhr, textStatus, errorThrown)
{
LogConsole_ajaxError(xhr, textStatus, errorThrown);
}
});
}
这是PhotoExist.jsp文件:
...
<% String path = request.getParameter("path");%>
...
<div class="divPhotoWrapper HavePhoto" onmouseover="ShowPhotoButton()" onmouseout="HidePhotoButton()">
<div class="divDeletePhoto displayNone">
<button class="buttonDeletePhoto" onclick="DeletePhoto()"></button>
</div>
<a id="aLogo">
<img id="imgLogo" src="<%=path + "?time=" +System.currentTimeMillis()%>" width="100" height="100">
</a>
</div>
...
这是servlet,它将上传的文件复制到备用文档根文件夹:
...
try
{
filePart = request.getPart("0");
String fullName = Common._FolderDatabase_LogoOrganization + File.separator + idOrganization + ".jpg";
File FileTemp = new File(fullName);
out = new FileOutputStream(FileTemp);
filecontent = filePart.getInputStream();
int read = 0;
final byte[] bytes = new byte[1024];
while ((read = filecontent.read(bytes)) != -1)
{
out.write(bytes, 0, read);
}
}
catch(IOException exception)
{
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Unable to copy file." + " Exception: ", exception);
response.sendError(HttpServletResponse.SC_ACCEPTED, "Unable to copy file.");
return;
}
finally
{
if (out != null)
out.close();
if (filecontent != null)
filecontent.close();
}
...
它工作良好,但有时(十分之一的尝试)浏览器无法下载图像源,并给出这个错误:GET http://localhost:8080/GetSome/Database/LogoOrganization/85.jpg?time=1407933088377 404 (Not Found)
。我尝试在servlet中检查文件是否可用,如下所示:
File FileTemp = new File(fullName);
while(!FileTemp.canWrite())
{
Thread.sleep(1000);
}
,像这样:
in = new FileInputStream(FileTemp);
int read = 0;
final byte[] bytes = new byte[1024];
while ((read = in.read(bytes)) != -1)
{
}
在这两种情况下,文件都是可用的,但它没有帮助。我猜Glassfish上传到文件夹后没有时间访问文件
我通过检查Glassfish资源的可用性来解决这个问题
URL url = getServletContext().getResource(requestUri);
if (url != null) {
try
{
Thread.sleep(100);
}
catch (InterruptedException ex)
{
Logger.getLogger(UploadLogo.class.getName()).log(Level.SEVERE, null, ex);
}
}
感谢你的回答
相关文章:
- 使用promise和mongoose对文档进行排序
- document.open/document.write没有正确地清除chrome中的文档——这是chrome的错误吗
- Ajax请求文档就绪会导致jquery加载缓慢
- MongoDB (php) - 以数组而不是多个属性的形式返回文档属性
- 谷歌文档表面引擎
- 来自文档或下一个静态父级的事件委派
- 如何将childNodes用于XML文档
- 文档就绪提供了错误的选择器高度
- 将当前用户的 ID 推送到 meteorjs 中集合/文档的内部数组
- 未捕获的语法错误:无法在“文档”上执行“查询选择器”
- BrowserId登录请求在文档加载时被调用
- 从Javascript和Php变量创建Html模板文档
- 如何通过谷歌应用程序脚本从谷歌文档中的位置确定命名范围
- 在jquery文档准备好之前加载Modernizr
- 如何判断何时将dom节点添加到文档中
- 无法获取文档.GetElementById工作正常
- 为什么是文档.旧版应用程序中的DOM-object.properties为null
- 有没有一种方法可以在没有文档或jQuery的情况下使用javascript解码html实体
- 是否存在Javascript Liferay Service库的文档?如何处理错误情况
- 上传文件到Glassfish备用文档根文件夹