我在这里读到,无论如何都不应该将文件保存在服务器中,因为它不是可移植的,事务性的并且需要外部参数.但是,鉴于我需要一个针对tomcat(7)的tmp解决方案,并且我对服务器机器有(相对)控制,我想知道:
保存文件的最佳位置是什么?我应该保存/WEB-INF/uploads(建议不要在这里)或某个地方$CATALINA_BASE(见这里)或......?JavaEE 6教程从用户获取路径(:wtf :).注意:该文件不应以任何方式下载.
我应该建立一个配置参数详见这里?我很欣赏一些代码(我宁愿给它一个相对路径 - 所以它至少是Tomcat可移植的) - Part.write()看起来很有希望 - 但显然需要一个绝对的路径
我对这种方法与数据库/ JCR存储库的缺点的阐述感兴趣
不幸的是 ,@ BalusC 的FileServlet专注于下载文件,而他在上传文件时的答案会跳过保存文件的位置.
容易转换为使用DB或JCR实现(如长耳兔)的解决方案将是优选的.
我们有简单的HTML表单<input type="file">,如下所示:
<form>
<label for="attachment">Attachment:</label>
<input type="file" name="attachment" id="attachment">
<input type="submit">
</form>
Run Code Online (Sandbox Code Playgroud)
在IE7(可能是所有着名的浏览器,包括旧的Firefox 2)中,如果我们提交像'// server1/path/to/file/filename'这样的文件,它可以正常工作,并提供文件和文件名的完整路径.
在Firefox 3中,它只返回'filename',因为它们有新的"安全功能"来截断路径,如Firefox错误跟踪系统(https://bugzilla.mozilla.org/show_bug.cgi?id=143220)中所述.
我不知道如何克服这个"新功能",因为它导致我的webapp中的所有上传表单停止在Firefox 3上运行.
任何人都可以帮助找到一个解决方案来获取Firefox 3和IE7上的文件路径?
我试图找到一个可以传递给f:ajax标签的属性"event"的所有可能值的列表.
我知道我也可以从我的.js文件中传递函数名称,但我需要的只是JSF附带的函数名称.
我只知道点击鼠标悬停和键盘,但我相信还有更多.只是不知道在哪里找到它们.
我正在寻找一些博客,试图找到如何使用JSF 2.0上传文件但是所有的解决方案都让我感到困惑.我想知道我需要什么才能成功上传文件(MP3,PDF,视频......什么类型)并将其作为@Lob存储在数据库中.这是我到目前为止所做的:
我创建了一个具有byte []类型属性的实体,并且还使用@Lob注释进行了注释.
我创建了一个EJB,它将使用一个byte []作为参数的方法引入实体,并使用EntityManager类(persist方法)将其插入到数据库中.
我创建了一个JSF页面,其中包含"file"类型的输入标记和一个提交按钮
我准备了一个托管bean来与JSF页面交换有关该文件的信息.
现在我被卡住了,我有很多疑惑:
我应该怎么做才能将文件从JSF传递到托管bean,然后将其转换为byte [](能够将其处理到EJB)?
servlet如何帮助我?
我需要一个servlet吗?
另外我发现在一些博客中它提到了一些关于servlets 3.0的东西,但我不知道我的工作环境是否正在使用它,如果我使用servlets 3.0(我使用的是JEE6)怎么办?
我之前从未上过文件,也不熟悉servlet.我很困惑,有人可以给我一些开始提示,拜托?
我在JSF上传文件.我正在使用Tomahawk <t:inputFileUpload>,但同样的问题适用于例如PrimeFaces <p:fileUpload>和JSF 2.2 <h:inputFile>.
我有以下支持bean代码:
private UploadedFile uploadedFile; // +getter+setter
public String save() throws IOException {
String name = uploadedFile.getName();
System.out.println("File name: " + name);
String type = uploadedFile.getContentType();
System.out.println("File type: " + type);
long size = uploadedFile.getSize();
System.out.println("File size: " + size);
InputStream stream = uploadedFile.getInputStream();
byte[] buffer = new byte[(int) size];
stream.read(buffer, 0, (int) size);
stream.close();
}
Run Code Online (Sandbox Code Playgroud)
我能够获取文件名,类型和大小,但我无法将此文件保存在特定路径中.我无法找出保存上传文件的正确方法.我怎样才能做到这一点?
我有一个JSF 2.2页面,下面的文件上传表单部署到WildFly 8.2.0.
<h:form>
<h:inputFile value="#{filePair.miscPart}" />
<h:commandButton value="submit" action="validationResult" />
</h:form>
Run Code Online (Sandbox Code Playgroud)
当我提交它时,我得到以下例外:
javax.servlet.ServletException: UT010016: Not a multi part request
at io.undertow.servlet.spec.HttpServletRequestImpl.loadParts(HttpServletRequestImpl.java:508)
at io.undertow.servlet.spec.HttpServletRequestImpl.getParts(HttpServletRequestImpl.java:459)
at com.sun.faces.renderkit.html_basic.FileRenderer.decode(FileRenderer.java:91)
at javax.faces.component.UIComponentBase.decode(UIComponentBase.java:831)
at javax.faces.component.UIInput.decode(UIInput.java:771)
at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1226)
at javax.faces.component.UIInput.processDecodes(UIInput.java:676)
at javax.faces.component.UIForm.processDecodes(UIForm.java:225)
at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1221)
at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1221)
at javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:929)
at com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:78)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61)
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:56)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.security.handlers.AuthenticationConstraintHandler.handleRequest(AuthenticationConstraintHandler.java:51)
at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45)
at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:63)
at io.undertow.servlet.handlers.security.ServletSecurityConstraintHandler.handleRequest(ServletSecurityConstraintHandler.java:56)
at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58)
at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70)
at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76) …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用h:inputFile标记上传图像文件并将其写入磁盘.
我的JSF代码:
<h:form id="fileUploadForm" enctype='multipart/form-data' prependId="false">
<h:inputFile value="#{solicitacaoManagedBean.imagemCarregada}" />
<br />
<h:commandButton styleClass="btn btn-primary " value="Enviar" action="#{solicitacaoManagedBean.enviarImagem}" />
</h:form>
Run Code Online (Sandbox Code Playgroud)
我的ManagedBean:
@Named(value = "solicitacaoManagedBean")
@SessionScoped
@MultipartConfig(location = "/home/rogerio/tmp/")
public class SolicitacaoManagedBean implements Serializable {
private Part imagemCarregada;
Run Code Online (Sandbox Code Playgroud)
(......)
public void enviarImagem() throws IOException {
try {
imagemCarregada.write("teste.jpg");
} catch (IOException ioe) {
System.out.println("Erro ao escrever: " + ioe.getLocalizedMessage());
}
}
}
Run Code Online (Sandbox Code Playgroud)
据推测,方法write将项目上传到磁盘,IOException也不会被抛出/捕获.
file-upload ×6
jsf ×5
jsf-2 ×2
jsf-2.2 ×2
ajax ×1
bytearray ×1
file-io ×1
firefox-3 ×1
html ×1
inputstream ×1
java ×1
save ×1
servlet-3.0 ×1
servlets ×1
tomcat ×1