Downloading
1 minute example: download
The following example shows how to expose the file to be downloaded to its client.
Again, see how simple this code is:
@Resource
public class ProfileController {
public File picture(Profile profile) {
return new File("/path/to/the/picture." + profile.getId()+ ".jpg");
}
}
Adding more info to download
If you want to add more information to download, you can return a FileDownload:
@Resource
public class ProfileController {
public Download picture(Profile profile) {
File file = new File("/path/to/the/picture." + profile.getId()+ ".jpg");
String contentType = "image/jpg";
String filename = profile.getName() + ".jpg";
return new FileDownload(file, contentType, filename);
}
}
1 minute example: upload
The first example is based on the multipart upload feature.
@Resource
public class ProfileController {
private final ProfileDao dao;
public ProfileController(ProfileDao dao) {
this.dao = dao;
}
public void updatePicture(Profile profile, UploadedFile picture) {
dao.update(picture.getFile(), profile);
}
}
More about Upload
UploadedFile returns the file content as a InputStream. If you want to save this file on disk in an
easy way, you can use the commons-io IOUtils, that is already a VRaptor dependency:
public void updatePicture(Profile profile, UploadedFile picture) {
File pictureOnDisk = new File();
IOUtils.copyLarge(picture.getFile(), new FileOutputStream(pictureOnDisk));
dao.atribui(pictureOnDisk, profile);
}