I'm using Wicket (not sure if it matters) but I'm using Workbook to create an excel file for a user to download. But I'm not sure how exactly to do this. What I would like to happen is the user clicks the button, a log is created and a prompt is given to the user to open (and save to temp files) or to save to their computer. The file is then deleted from the server side, or maybe it is stored in the User's session and deleted at end of session.
Can someone point me in the right direction? If I can have the file not saved in the session at all, that'd be create and have it just have it sent to the client using FileOutputStream somehow..
here is my current code:
private void excelCreator()
{
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet(WorkbookUtil.createSafeSheetName("SSA User ID " + currentSSAIDSelection2.getSsaUserId()));
Iterator<AuditLogEntry> auditLogEntrys = logList.iterator();
int i = 0;
while (auditLogEntrys.hasNext())
{
final AuditLogEntry auditLogEntry = auditLogEntrys.next();
Row row = sheet.createRow(i);
row.createCell(0).setCellValue(auditLogEntry.getTimeStamp());
row.createCell(1).setCellValue(auditLogEntry.getSourceName());
row.createCell(2).setCellValue(auditLogEntry.getCategory());
row.createCell(3).setCellValue(auditLogEntry.getSsaAdmin());
row.createCell(4).setCellValue(auditLogEntry.getAction());
i++;
}
try
{
FileOutputStream output = new FileOutputStream("ssaUserIDAccess.xls");
workbook.write(output);
output.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
You would have to create a DownloadLink with the temporary file as input. The temporary File must be deleted after download (file.delete())).
Alternatively you can try this:
IResourceStream stream = new ByteArrayResourceStream(data, "application/vnd.ms-excel");
RequestCycle.get().scheduleRequestHandlerAfterCurrent(new ResourceStreamRequestHandler(stream, filename).setContentDisposition(ContentDisposition.ATTACHMENT));
In this case data is the byte[] content of your workbook which can be for example retrieved with output.toByteArray().
In case anyone runs into this problem here is my solution. There wasn't a lot of straight forward answers on this but this is my solution:
My excelCreator method handles the creation of the excel Sheet, and returns it as a file.
private File excelCreator()
{
Workbook workbook = new HSSFWorkbook();
File excelfile = new File("userIDAccess.xls");
logList = getServer().findAuditLogs(getUserId(), null);
Sheet sheet = workbook.createSheet(WorkbookUtil.createSafeSheetName("User ID " + getUserId()));
Iterator<AuditLogEntry> auditLogEntrys = logList.iterator();
int i = 0;
while (auditLogEntrys.hasNext())
{
final AuditLogEntry auditLogEntry = auditLogEntrys.next();
Row row = sheet.createRow(i);
row.createCell(0).setCellValue(auditLogEntry.getTimeStamp());
row.createCell(1).setCellValue(auditLogEntry.getSourceName());
row.createCell(2).setCellValue(auditLogEntry.getCategory());
row.createCell(3).setCellValue(auditLogEntry.getSsaAdmin());
row.createCell(4).setCellValue(auditLogEntry.getAction());
i++;
}
try
{
FileOutputStream output = new FileOutputStream(excelfile);
workbook.write(output);
output.close();
}catch(Exception e)
{
e.printStackTrace();
}
return excelfile;
}
IModel excelFileModel = new AbstractReadOnlyModel()
{
public Object getObject()
{
return excelCreator();
}
};
I created an IModel to capture the file created inside my excelCreator() method and returned.
auditDownloadlink = new DownloadLink("auditDownloadlink", excelFileModel);
I pass the I.D. of the download link, and then pass the imodel.
finally,
I call,
auditDownloadlink.setDeleteAfterDownload(true);
auditDownloadlink.setCacheDuration(Duration.NONE);
This deletes the file after it is created. And the cache setting is a setting to make sure it is compatible with all browsers (That's how I interpreted it, but you may not need it).
The Imodel creates the File on the fly so it doesn't have to be stored anywhere, and then the file is deleted once it is downloaded.
Hope this helps someone!
You could create a Resource to do this, and make a ResourceLink.
public class ExcelProducerResource extends AbstractResource
{
public ExcelProducerResource()
{
}
#Override
protected ResourceResponse newResourceResponse( Attributes attributes )
{
final String fileName = getFileName();
ResourceResponse resourceResponse = new ResourceResponse();
resourceResponse.setContentType( "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" );
resourceResponse.setCacheDuration( Duration.NONE );
resourceResponse.setFileName( fileName );
resourceResponse.setWriteCallback( new WriteCallback()
{
#Override
public void writeData( Attributes attributes ) throws IOException
{
OutputStream outputStream = attributes.getResponse().getOutputStream();
writeToStream( outputStream );
outputStream.close();
}
} );
return resourceResponse;
}
void writeToStream(OutputStream outputStream) throws IOException
{
//.. do stuff here :)
}
String getFileName()
{
//.. do stuff here :)
}
}
Related
Whenever Image is uploaded to the Stock item page, I have to resize the image to thump size and upload the copy of the same.
How do I override the upload function to add my logic?
The upload function is HandleUpload of the PXImageUploader control in PX.Web.UI.
You can try to overwrite that function and then replace the control on the page with yours.
Another way will be to handle the resizing inside the InventoryItemMaint graph.
You can check the ImageUrl field update and do the resize there. Any time you upload a new picture the URL is basically updated. Please don't use the example below in production as it was never fully tested.
// Acuminator disable once PX1016 ExtensionDoesNotDeclareIsActiveMethod extension should be constantly active
public class InventoryItemMaintExt : PXGraphExtension<InventoryItemMaint>
{
public PXSelect<UploadFileRevision> uploadFileRevisions;
protected virtual void InventoryItem_ImageUrl_FieldUpdated(PXCache sender, PXFieldUpdatedEventArgs e, PXFieldUpdated baseMethod)
{
baseMethod?.Invoke(sender, e);
if(e.Row is InventoryItem row)
{
if ((string)e.OldValue != row.ImageUrl) //ADD conditions so that this doesn't work any time user change the image if there are multiple attached
{
UpdateImageFileRevisionToResizedImage(sender, row);
}
}
}
private void UpdateImageFileRevisionToResizedImage(PXCache sender, InventoryItem row)
{
var fileNotes = PXNoteAttribute.GetFileNotes(sender, row);
UploadFileRevision uploadedFile = GetFile(sender.Graph, fileNotes, row.ImageUrl);
if (uploadedFile != null)
{
var data = ResizeImage(uploadedFile.Data);
uploadedFile.Data = data;
uploadFileRevisions.Update(uploadedFile);
}
}
//WARNING: DON'T USE THIS METHOD IN PRODUCTION.
// USE ANY OTHER RECOMMENDED METHOD TO RESIZE IMAGES
private static byte[] ResizeImage(byte[] data)
{
System.IO.MemoryStream myMemStream = new System.IO.MemoryStream(data);
System.Drawing.Image fullsizeImage = System.Drawing.Image.FromStream(myMemStream);
System.Drawing.Image newImage = fullsizeImage.GetThumbnailImage(200, 200, null, IntPtr.Zero);
System.IO.MemoryStream myResult = new System.IO.MemoryStream();
newImage.Save(myResult, System.Drawing.Imaging.ImageFormat.Jpeg); //Or whatever format you want.
return myResult.ToArray(); //Returns a new byte array.
}
private static UploadFileRevision GetFile(PXGraph graph, Guid[] fileIds,string fileUrl)
{
return (UploadFileRevision)PXSelectBase<UploadFileRevision,
PXSelectJoin< UploadFileRevision,
InnerJoin <UploadFile,
On<UploadFile.fileID, Equal<UploadFileRevision.fileID>,
And<UploadFile.lastRevisionID, Equal<UploadFileRevision.fileRevisionID>>>>,
Where<UploadFile.fileID, In<Required<UploadFile.fileID>>,
And<UploadFile.name,Equal<Required<UploadFile.name>>>>>.Config>.Select(graph, new object[]
{
fileIds,
fileUrl
});
}
}
I am running into a strange null pointer exception when attempting to create a sheet using ApachePOI. I require a more modular code as this is part of a larger system. I have been unable to find any similar examples online, all examples are stored within a main method.
I am also wondering when it is necessary to include a workbook.write(outputStream) does this need to be in every method that a new sheet/workbook/row/cell is created or only when I am writing data to the sheets (like in the write method)?
public class ExcelWriter {
private XSSFWorkbook workbook;
private String outputFile;
private ArrayList<XSSFSheet> sheets;
private static int sheetCount;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Constructor --> initializes a workbook
public ExcelWriter(String outputFile) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
this.outputFile=outputFile;
sheets=new ArrayList<XSSFSheet>();
sheetCount=0;
FileOutputStream os = new FileOutputStream(outputFile);
workbook.write(os);
os.close();
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Method to create a new sheet in the workbook, add to sheet list
public void newSheet(String sheetName) throws IOException {
sheets.add(workbook.createSheet(sheetName)); //ERROR HERE****
sheetCount++;
FileOutputStream os = new FileOutputStream(outputFile);
workbook.write(os);
os.close();
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Method to write a cell into a worksheet given sheet row and column
public void write(int sheet,int row, int col, String data) {
XSSFSheet tempSheet=sheets.get(sheet);
System.out.println("yest");
// Specific row number
XSSFRow tempRow = tempSheet.createRow(row);
// Specific cell number
XSSFCell cell = tempRow.createCell(col);
// putting value at specific position
cell.setCellValue(data);
// writing the content to Workbook
OutputStream os;
try {
os = new FileOutputStream(outputFile);
workbook.write(os);
} catch (IOException e) {
e.printStackTrace();
}
}
i am writing my own image import for my product catalog. I want to read the images from the local filesystem and store them in the configured assets folder. The import is very simple for now. Its one controller in the admin project and i trigger it by calling an url.
It is creating the files along with the folder structure and the files seem to have the same filesize, but somehow they get messed up along the way and they are not readable as images anymore (picture viewers wont open them). Any ideas why its being messed up ?
here the code:
#Controller("blImageImportController")
#RequestMapping("/imageimport")
public class ImageImportController extends AdminAbstractController {
#Value("${image.import.folder.location}")
private String importFolderLocation;
#Resource(name = "blStaticAssetService")
protected StaticAssetService staticAssetService;
#Resource(name = "blStaticAssetStorageService")
protected StaticAssetStorageService staticAssetStorageService;
#RequestMapping(method = {RequestMethod.GET})
public String chooseMediaForMapKey(HttpServletRequest request,
HttpServletResponse response,
Model model
) throws Exception {
File imageImportFolder = new File(importFolderLocation);
if (imageImportFolder.isDirectory()) {
Arrays.stream(imageImportFolder.listFiles()).forEach(directory ->
{
if (directory.isDirectory()) {
Arrays.stream(directory.listFiles()).forEach(this::processFile);
}
});
}
return "";
}
private void processFile(File file) {
FileInputStream fis = null;
try {
HashMap properties = new HashMap();
properties.put("entityType", "product");
properties.put("entityId", file.getParentFile().getName());
fis = new FileInputStream(file);
StaticAsset staticAsset = this.staticAssetService.createStaticAsset(fis, file.getName(), file.length(), properties);
this.staticAssetStorageService.createStaticAssetStorage(fis, staticAsset);
fis.close();
} catch (Exception e) {
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
There is a check in the StaticAssetService to try to detect this as an image (see https://github.com/BroadleafCommerce/BroadleafCommerce/blob/b55848f/admin/broadleaf-contentmanagement-module/src/main/java/org/broadleafcommerce/cms/file/service/StaticAssetServiceImpl.java#L217-L220). If it detected this correctly, you should get back an ImageStaticAssetImpl in the result to that call.
The flipside of this is the controller that actually reads the file (the StaticAssetViewController that renders a StaticAssetView). One of the things that the StaticAssetView does is set a response header for mimeType which the browser uses to render. This is set by this piece in the StaticAssetStorageService: https://github.com/BroadleafCommerce/BroadleafCommerce/blob/b55848f837f26022a620f0c2c143eed7902ba3f1/admin/broadleaf-contentmanagement-module/src/main/java/org/broadleafcommerce/cms/file/service/StaticAssetStorageServiceImpl.java#L213. I suspect that is the root of your problem.
Also just a note, sending those properties is not necessary when you are uploading the file yourself. That is mainly used in the admin when you are uploading an image for a specific entity (like a product or a category).
my web application offers a download. Javascript creats at the click the url (it depends on the user input) and the browser should open it, so that the page isn't reloaded.
For that, I think I have to alternatives:
// Alt1:
window.open(pathToFile);
// Alt2:
var downloadFrame = document.getElementById('downloads');
if (downloadFrame === null) {
downloadFrame = document.createElement('iframe');
downloadFrame.id = 'downloads';
downloadFrame.style.display = 'none';
document.body.appendChild(downloadFrame);
}
downloadFrame.src = pathToFile;
Both works under Firefox. Problem with open new window method: If the creation of the file at the server needs more time, the new empty tab will be closed late.
Problem with iframe: If there is an error at the server, no feedback is given.
I think at firefox the iframe is the better solution. But the web application must run with an JavaFX WebView, too. JavaFX haven't a download feature, I have to write it. One possible way is to listen on the location property:
final WebView webView = new WebView();
webView.getEngine().locationProperty().addListener(new ChangeListener<String>() {
#Override public void changed(ObservableValue<? extends String> observableValue, String oldLoc, String newLoc) {
if (newLoc.cotains("/download")) {
FileChooser chooser = new FileChooser();
chooser.setTitle("Save " + newLoc);
File saveFile = chooser.showSaveDialog(webView.getEngine().getScene().getWindow());
if (saveFile != null) {
BufferedInputStream is = null;
BufferedOutputStream os = null;
try {
is = new BufferedInputStream(new URL(newLoc).openStream());
os = new BufferedOutputStream(new FileOutputStream(saveFile));
while ((readBytes = is.read()) != -1) {
os.write(b);
}
} finally {
try { if (is != null) is.close(); } catch (IOException e) {}
try { if (os != null) os.close(); } catch (IOException e) {}
}
}
}
}
}
There are some problems:
The download start depends on a part of the url, because JafaFX supports no access to the http headers (that is bearable)
If the user starts the download with the same url two times, only the first download works (the change event only fires, if the url is new). I can crate unique urls (with #1, #2 and so on at the end). But this is ugly.
Only the "window.open(pathToFile);" method works. Loading an iframe don't fire the change location event of the website. That is expectable but I haven't found the right Listener.
Can someone help me to solve 2. or 3.?
Thank you!
PS: Sorry for my bad english.
edit:
For 2. I found a way. I don't know if it is a good one, if it is performant, if the new webview is deleted or is in the cache after download, ....
And the user don't get an feedback, when some a problem is raised:
webView.getEngine().setCreatePopupHandler(new Callback<PopupFeatures, WebEngine>() {
#Override public WebEngine call(PopupFeatures config) {
final WebView downloader = new WebView();
downloader.getEngine().locationProperty().addListener(/* The Listener from above */);
return downloader.getEngine();
}
}
I think you may just need to use copyURLtoFile to get the file...call that when the location changes or just call that using a registered java class. Something like this:
org.apache.commons.io.FileUtils.copyURLToFile(new URL(newLoc), new File(System.getProperty("user.home")+filename));
Using copyURLToFile the current page doesn't have to serve the file. I think registering the class is probably the easiest way to go... something like this:
PHP Code:
Download $filename
Java (in-line class in your javafx class/window... in this case my javafx window is inside of a jframe):
public class JavaApp {
JFrame cloudFrameREF;
JavaApp(JFrame cloudFrameREF)
{
this.cloudFrameREF = cloudFrameREF;
}
public void getfile(String filename) {
String newLoc = "http://your_web_site.com/send_file.php?filename=" + filename;
org.apache.commons.io.FileUtils.copyURLToFile(new URL(newLoc), new File(System.getProperty("user.home")+filename));
}
}
This part would go in the main javafx class:
Platform.runLater(new Runnable() {
#Override
public void run() {
browser2 = new WebView();
webEngine = browser2.getEngine();
appREF = new JavaApp(cloudFrame);
webEngine.getLoadWorker().stateProperty().addListener(
new ChangeListener<State>() {
#Override public void changed(ObservableValue ov, State oldState, State newState) {
if (newState == Worker.State.SUCCEEDED) {
JSObject win
= (JSObject) webEngine.executeScript("window");
// this next line registers the JavaApp class with the page... you can then call it from javascript using "app.method_name".
win.setMember("app", appREF);
}
}
});
You may not need the frame reference... I was hacking some of my own code to test this out and the ref was useful for other things...
I'm creating a GUI, and I use a method "getStudentInfo()" of the Student object return data type to retrieve information from the JTextFields and storing them into the "student" object.
public Student getStudentInfo() {
Student student = new Student();
String name = jtfName.getText();
student.setName(name);
String idNumber = jtfIDNumber.getText();
student.setIdNumber(idNumber);
String address = jtfAddress.getText();
student.setAddress(address);
String phoneNumber = jtfPhoneNumber.getText();
student.setPhoneNumber(phoneNumber);
String major = jtfMajor.getText();
student.setMajor(major);
return student;
}
Then, in a different class, I create an "Add" button that, when clicked, is supposed to add the "student" object into an ArrayList, and then write the ArrayList into a binary file.
private class AddButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
File studentFile = new File(FILENAME);
ArrayList<Student> studentList = new ArrayList<Student>();
studentList.add(text.getStudentInfo());
try {
FileOutputStream fos = new FileOutputStream(studentFile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(studentList);
}
catch (FileNotFoundException fnf) {
fnf.printStackTrace();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
But when I run the program and I write a student's info and add it to the binary file, then I go to add another student, it overwrites the previous student's info completely. Any help would be greatly appreciated.
In the actionPerformed method of your class, AddButtonListener, you have the following line of code:
FileOutputStream fos = new FileOutputStream(studentFile);
This constructor will open the file so bytes are written to the beginning of your file. Since you reopen this file each time the button is clicked, you are replacing the file contents with new data. Instead, use the constructor with the boolean parameter for appending bytes rather than overwriting...
FileOutputStream fos = new FileOutputStream(studentFile, true);
You can check out this constructor's details in the java documentation...
FileOutputStream constructor documentation