Você está na página 1de 5

File Upload/Download HOWTO

This document covers the simplest upload & download cases (with storage on the local filesystem), and then extends those simple examples to cover storage in a database. This is the script I followed during todays File Upload/Download Master Tutorial, updated for 4.0. You should have some knowledge of WaveMaker already; especially dropping widgets, creating database services, and some knowledge of Java. It is organized into 4 sections; each section builds upon the previous ones. Sample projects are included for the results of each section.

UPLOAD
Well begin by creating a project that uploads files to the servers local disk. 1. Create a new project (named anything.) 2. Create a new Java Service. Both the serviceId and className can be anything; this guide will assume a serviceId of fileTransfer and a className of FileTransfer. 3. Now, well add a method to the Java Service. When youve pasted this in, hit the save button and make sure everything works. Windows users, youll have to change the location of UPLOADS; Mac and Linux users should be fine, but they might choose a more appropriate location. Make sure that directory exists, too.
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import org.apache.commons.io.IOUtils; import org.springframework.web.multipart.MultipartFile; import com.wavemaker.runtime.server.ParamName; /** * This is a client-facing service class. All * public methods will be exposed to the client. Their return * values and parameters will be passed to the client or taken * from the client, respectively. This will be a singleton * instance, shared between all requests. */ public class FileTransfer {

/** UPLOADS stores the location of files on disk. users

Windows

* must update this; Mac & Linux should be fine. */ private File UPLOADS = new File("/tmp/uploads"); public void doUpload(@ParamName(name="file") MultipartFile file) throws IOException { File outputFile = new File(UPLOADS, file.getOriginalFilename()); System.out.println("writing the content of uploaded file to: "+outputFile); FileOutputStream fos = new FileOutputStream(outputFile); IOUtils.copy(file.getInputStream(), fos); file.getInputStream().close(); fos.close(); } }

4. Now, switch to the designer. Add a FileUpload widget, and set the service and operation to match the JavaService service name, and the method we just created in the Java Service.

5. Hit run, and test your new file upload widget. You should see files appear in where you set the UPLOAD variable.

DOWNLOAD
1. Copy the project to a new one. 2. Add the following imports into the top of your JavaService file.

import com.wavemaker.runtime.server.DownloadResponse; import java.io.FileInputStream;

3. Add this new method into your JavaService, and hit save. Everything should work.
public DownloadResponse doDownload(@ParamName(name="filename") String filename) throws IOException { DownloadResponse ret = new DownloadResponse(); File localFile = new File("/tmp/uploads", filename); FileInputStream fis = new FileInputStream(localFile); ret.setContents(fis); ret.setFileName(filename); return ret; }

4. Switch back to the designer, and drop a new editor, with an onchange javascript like this:
editor1Change: function(inSender, inDisplayValue, inDataValue) { this.label1.setLink("services/fileTransfer.download?method= doDownload&filename="+inDisplayValue); },

5. Drop a new label (label1), change the label to download 6. Hit run to test your new download widget. You should enter the name of a file youve already uploaded, blur out of the editor widget, and then hit the download link to try to the download.

UPLOAD TO DB
1. Import the database (filedb.sql) into your MySQL installation, via something like this:
> cat filedb.sql | mysql -u root

2. In the datamodel editor, import that database into your project. 3. Switch to the queries tab, make a new query called getMaxId, with the query string:
select max(id) from Files

4. Hit run to generate the database service file (while the table files are created on import, you need to run before the service class is generated & compiled.) 5. Edit the java service, add:

import com.filedb.Filedb; import com.filedb.data.Files; import com.wavemaker.runtime.RuntimeAccess;

6.Replace the old doUpload method with this new one:


public void doUpload(@ParamName(name="file") MultipartFile file) throws IOException { Filedb filedb = (Filedb) RuntimeAccess.getInstance().getService("filedb"); Files f = new Files(); f.setFilename(file.getOriginalFilename()); f.setId(filedb.getMaxId()+1); System.out.println("writing the content of uploaded file to: "+f.getFilename()); f.setContents(IOUtils.toByteArray(file.getInputStream())); filedb.insert(f); file.getInputStream().close(); }

7. Test your upload by running your application, try the upload widget (the same widget we created above), and make sure your database has the new rows.

DOWNLOAD FROM DB
1. Add a new query, named getFilesByName, and with the query string:
from Files _a where _a.filename=:filename

It should have a single bind parameter filename, type String. 2. Add a new import to the Java service:
import java.io.ByteArrayInputStream;

3. Replace the existing doDownload method with this one:


public DownloadResponse doDownload(@ParamName(name="filename") String filename) throws IOException { Filedb filedb = (Filedb) RuntimeAccess.getInstance().getService("filedb"); DownloadResponse ret = new DownloadResponse(); Files file = filedb.getFilesByName(filename); ret.setContents(new ByteArrayInputStream(file.getContents())); ret.setFileName(file.getFilename()); return ret;

4. Run your application, and verify that the download widget still works, and that it retrieves information from the database (maybe by clearing our the upload directory on disk.)

Você também pode gostar