• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Not able to upload images on server

 
Greenhorn
Posts: 2
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I'm new in java world.
I'm working on JSF framework
so please help me,

I working on admin module on E-Greeting card project, like 123greetings.

Images store in project folder not in database. In database only link path store.

I'm Not able to store images on project folder. Images store temporary on server.
so please help me,

here i attach the code

[1] This JSF code.



<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>

<%@taglib prefix="rich" uri="http://richfaces.org/rich" %>
<%@taglib prefix="a4j" uri="http://richfaces.org/a4j"%>




<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">




<f:view>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Birthday Card</title>
</head>
<body>
<h1><h:outputText value="Upload Image"/></h1>


<h:form>
<h:panelGrid columns="2" columnClasses="top,top">
<rich:fileUpload fileUploadListener="#{fileUploadBean.listener}"
maxFilesQuantity="#{fileUploadBean.uploadsAvailable}"
id="upload"
immediateUpload="#{fileUploadBean.autoUpload}"
acceptedTypes="jpg, gif, png, bmp" allowFlash="#{fileUploadBean.useFlash}">
<a4j:support event="onuploadcomplete" reRender="info" />
</rich:fileUpload>
<h:panelGroup id="info">
<rich:panel bodyClass="info">
<f:facet name="header">
<h:outputText value="Uploaded Files Info" />
</f:facet>
<h:outputText value="No files currently uploaded"
rendered="#{fileUploadBean.size==0}" />
<rich:dataGrid columns="1" value="#{fileUploadBean.files}"
var="file" rowKeyVar="row">
<rich:panel bodyClass="rich-laguna-panel-no-header">
<h:panelGrid columns="2">
<a4j:mediaOutput element="img" mimeType="#{file.mime}"
createContent="#{fileUploadBean.paint}" value="#{row}"
style="width:100px; height:100px;" cacheable="false">
<f:param value="#{fileUploadBean.timeStamp}" name="time"/>
</a4j:mediaOutput>
<h:panelGrid columns="2">
<h:outputText value="File Name:" />
<h:outputText value="#{file.name}" />
<h:outputText value="File Length(bytes):" />
<h:outputText value="#{file.length}" />
</h:panelGrid>
</h:panelGrid>
</rich:panel>
</rich:dataGrid>
</rich:panel>
<rich:spacer height="3"/>


<a4j:commandButton action="#{fileUploadBean.clearUploadData}"
reRender="info, upload" value="Clear Uploaded Data"
rendered="#{fileUploadBean.size>0}" />
</h:panelGroup>
</h:panelGrid>
</h:form>

</body>
</html>
</f:view>



[2] This is bean file of Jsf
FileUploadBean.java


import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

import org.richfaces.event.UploadEvent;
import org.richfaces.model.UploadItem;

/**
* @author Ilya Shaikovsky
*
*/
public class FileUploadBean{

private ArrayList<FileTest> files = new ArrayList<FileTest>();
private int uploadsAvailable = 1000;
private boolean autoUpload = true;
private boolean useFlash = true;
private boolean createTempFiles = true;

public int getSize() {
if (getFiles().size()>0){
return getFiles().size();
}else
{
return 0;
}
}



public FileUploadBean() {
}

public void paint(OutputStream stream, Object object) throws IOException {
stream.write(getFiles().get((Integer)object).getData());
}
public void listener(UploadEvent event) throws Exception{
UploadItem item = event.getUploadItem();
FileTest file = new FileTest();
file.setLength(item.getData().length);
file.setName(item.getFileName());
file.setData(item.getData());

files.add(file);


uploadsAvailable++;
}



public String clearUploadData() {
files.clear();
setUploadsAvailable(1000);
return null;
}

public long getTimeStamp(){
return System.currentTimeMillis();
}

public ArrayList<FileTest> getFiles() {
return files;
}

public void setFiles(ArrayList<FileTest> files) {
this.files = files;
}

public int getUploadsAvailable() {
return uploadsAvailable;
}

public void setUploadsAvailable(int uploadsAvailable) {
this.uploadsAvailable = uploadsAvailable;
}

public boolean isAutoUpload() {
return autoUpload;
}

public void setAutoUpload(boolean autoUpload) {
this.autoUpload = autoUpload;
}

public boolean isUseFlash() {
return useFlash;
}

public void setUseFlash(boolean useFlash) {
this.useFlash = useFlash;
}

public boolean iscreateTempFiles(){
return createTempFiles;
}

public void setcreateTempFiles(boolean createTempFiles){
this.createTempFiles = createTempFiles;
}
}


[3]this FileTest.java


public class FileTest {

private String Name;
private String mime;


private long length;
private byte[] data;





public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
int extDot = name.lastIndexOf('.');
if(extDot > 0){
String extension = name.substring(extDot +1);
if("bmp".equals(extension)){
mime="image/bmp";
} else if("jpg".equals(extension)){
mime="image/jpeg";
} else if("gif".equals(extension)){
mime="image/gif";
} else if("png".equals(extension)){
mime="image/png";
} else {
mime = "image/unknown";
}
}
}
public long getLength() {
return length;
}
public void setLength(long length) {
this.length = length;
}



public String getMime(){
return mime;
}

public byte[] getData() {
return data;
}
public void setData(byte[] data) {
this.data = data;
}


}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic