我想上传一个图像并将其存储在服务器上,然后用h:graphicImage显示它?我想将它存储在应用程序的"资源/图像"中.我正在使用glassfish 4.现在,文件转到"domain1\generated\jsp\FileUpload".谢谢
我的表格
<h:form id="form" enctype="multipart/form-data">
<h:messages/>
<h:panelGrid columns="2">
<h:outputText value="File:"/>
<h:inputFile id="file" value="#{uploadPage.uploadedFile}"/>
</h:panelGrid>
<br/><br/>
<h:commandButton value="Upload File" action="#{uploadPage.uploadFile}"/>
</h:form>
Run Code Online (Sandbox Code Playgroud)
我的豆子
@Named
@ViewScoped
public class UploadPage {
private Part uploadedFile;
public void uploadFile(){
File file = File.createTempFile("somefilename-", ".jpg", new File("C:\\var\\webapp\\images"));
uploadedFile.write(file.getAbsolutePath());
}
}
Run Code Online (Sandbox Code Playgroud) 我无法得到primeFaces的<p:filedownload工作.我成功使用自定义下载bean下载文件.似乎issu与primefaces文件下载ActionListener有关,但我没有看到如何.有人有线索吗?谢谢.
这是我的豆子:
@ManagedBean(name="fileDownloadBean")
@SessionScoped
public class FileDownloadBean implements Serializable {
private StreamedContent file;
public StreamedContent getFile() {
return file;
}
public void setFile(StreamedContent file) {
this.file = file;
}
/** Creates a new instance of FileDownloadBean */
public FileDownloadBean() {
InputStream stream = this.getClass().getResourceAsStream("sessionlog.csv");
file = new DefaultStreamedContent(stream, "application/csv", "sessionlog.csv");
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的观看代码:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.prime.com.tr/ui"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:o="http://openfaces.org/"
template="./../template.xhtml">
<ui:define name="title">
#{bundle.Log}
</ui:define>
<ui:define …Run Code Online (Sandbox Code Playgroud) 我有一个servlet,它读取init()上的.properties文件.我的代码(不是下面的代码)可以工作,如果我在我的web.xml中设置了一个context-parameter,但我读到一个context-parameter是全局可访问的,我不希望这个,因为这个servlet只是一个更大的一块Web应用程序.我只是希望能够使用我试过的init-param标签来做到这一点:
public void init(ServletConfig config) throws ServletException {
try {
String fileName = config.getInitParameter("configFile");
System.out.println(fileName);
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
p = new Properties();
p.load(fis);
} catch (IOException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
但我一直得到文件未找到异常.我搜索过互联网,但大多数人都使用servlet上下文.如何在不在web.xml中包含context-param标记的情况下加载我的属性文件?
谢谢!
编辑:
java.io.FileNotFoundException: C:\WEB-INF\classes\myapp.properties (The system cannot find the path specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at java.io.FileInputStream.<init>(FileInputStream.java:79)
at ipadService.ProxyServlet.init(ProxyServlet.java:53)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1206)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:827)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at …Run Code Online (Sandbox Code Playgroud) 在使用JSF的项目中,我们有JavaScript文件位于:
MyProject/view/javascript/*.js
Run Code Online (Sandbox Code Playgroud)
在xhtml文件中,我将这些资源包括在内
<script type="text/javascript" src="javascript/#{myBean.jsFileName}.js" />
Run Code Online (Sandbox Code Playgroud)
这工作正常,但它可能#{myBean.jsFileName}.js不存在,在这种情况下我想加载"Default.js".
在myBean中(或在xhtml文件本身中),如何在设置#{myBean.jsFileName}值之前首先检查js文件是否存在?我尝试过这种变化:
File f = new File("javascript/myFile.js");
if (!f.exists){ jsFileName="Default"};
Run Code Online (Sandbox Code Playgroud)
但我不认为这是对的.我是在正确的轨道上吗?
我想为我的网络应用程序创建一个授权过滤器(以便能够限制对某些页面的访问).
我创建了一个简单的.xml文件,其中包含允许每个用户访问的页面:
<access>
<buyer>
<page>buyoffer.xhtml</page>
<page>faq.xhtml</page>
<page>index.jsp</page>
<page>login.xhtml</page>
<page>main.xhtml</page>
<page>registrationSucceded.xhtml</page>
</buyer>
<seller>
<page>sellerpanel.xhtml</page>
<page>faq.xhtml</page>
<page>index.jsp</page>
<page>login.xhtml</page>
<page>main.xhtml</page>
<page>registrationSucceded.xhtml</page>
</seller>
<administrator>
<page>sellerpanel.xhtml</page>
<page>faq.xhtml</page>
<page>index.jsp</page>
<page>login.xhtml</page>
<page>main.xhtml</page>
<page>registrationSucceded.xhtml</page>
</administrator>
</access>
Run Code Online (Sandbox Code Playgroud)
然后我需要进行解析以提取页面的值,以便能够创建允许或重定向的条件(依赖).我只需要告诉某人如何从xml中提取这些页面的值.这就是我现在所做的:
public class RestrictPageFilter implements Filter {
private FilterConfig fc;
private DocumentBuilder builder;
private Document document;
public void init(FilterConfig filterConfig) throws ServletException {
// The easiest way to initialize the filter
fc = filterConfig;
// Get the file that contains the allowed pages
File f = new File("/allowedpages.xml");
// Prepare the file …Run Code Online (Sandbox Code Playgroud) 我对输入和输出流有这个奇怪的事情,我只是无法理解.我使用inputstream从这样的资源中读取属性文件:
Properties prop = new Properties();
InputStream in = getClass().getResourceAsStream( "/resources/SQL.properties" );
rop.load(in);
return prop;
Run Code Online (Sandbox Code Playgroud)
它找到我的文件并成功更新它.我尝试写这样的修改设置:
prop.store(new FileOutputStream( "/resources/SQL.properties" ), null);
Run Code Online (Sandbox Code Playgroud)
我收到奇怪的错误:
java.io.FileNotFoundException: \resources\SQL.properties (The system cannot find the path specified)
Run Code Online (Sandbox Code Playgroud)
那么为什么改变物业的道路呢?如何解决这个问题?我在Windows上使用Netbeans
我目前在我的.war文件中有一堆图像.
WAR-ROOT
-WEB-INF
-IMAGES
-image1.jpg
-image2.jpg
-index.html
Run Code Online (Sandbox Code Playgroud)
当我通过我的servlets/jsp/etc生成html时,我可以简单地链接到
HTTP://host/contextroot/IMAGES/image1.jpg
和
HTTP://host/contextroot/IMAGES/image1.jpg
不是我正在编写一个需要获取文件系统引用这些图像的servlet(在这种情况下呈现复合.pdf文件).有没有人建议如何获得文件系统引用放在战争中的文件类似于这是什么?
它可能是我抓住servlet初始化的URL吗?我显然可以有一个明确指向已安装目录的属性文件,但我想避免其他配置.
我正在上传一个文件,我想提供一个相对路径,因为该程序应该在linux和windows环境下工作.
这就是我用来上传的内容
realPath = getServletContext().getRealPath(/files);
destinationDir = new File(realPath);
if(!item.isFormField())
{
File file = new File(destinationDir,item.getName());
item.write(file);
}
Run Code Online (Sandbox Code Playgroud)
在这里直接提供相对路径的任何其他方法
File file = new File(destinationDir,item.getName());
Run Code Online (Sandbox Code Playgroud) 我正在使用eclipse制作动态网页.它是一个依赖servlet传递/检索数据的简单程序.我的一个servlet必须打开.txt文件来读取其内容并将其发送到客户端.但是我收到了FileNotFound异常.我知道它,因为我不知道如何/在哪里放置txt文件,以便servlet可以在运行时找到该文件.我正在研究日食.你能提供一些提示吗?
谢谢
我需要从类中访问我的Web项目中的资源文件.问题是开发时的路径与部署路径不同.
例如,如果我想在开发时访问某些css文件,我可以这样做:
File file = new File("src/main/webapp/resources/styles/some.css/");
Run Code Online (Sandbox Code Playgroud)
但是一旦部署它可能不起作用,因为目标文件夹中没有src或main目录.那么如何一致地访问文件呢?