/
10-download-and-upload.afc
101 lines (69 loc) · 2.8 KB
/
10-download-and-upload.afc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
[chapter Download and Upload]
[section 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:
[java]
@Resource
public class ProfileController {
public File picture(Profile profile) {
return new File("/path/to/the/picture." + profile.getId()+ ".jpg");
}
}
[/java]
[section Adding more info to download]
If you want to add more information to download, you can return a FileDownload:
[java]
@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);
}
}
[/java]
You can also use the %%InputStreamDownload%% implementation to work with Streams or
%%ByteArrayDownload%% to work with byte array (since 3.2-snapshot).
[section Upload]
The upload component is optional. To enable this feature, you only need to add the %%commons-upload%% library in your classpath.
Since VRaptor 3.2, if you're in a container that implements the JSR-315 (Servlet 3.0), you don't need %%commons-upload%% nor
%%commons-io%% libraries because the servlet container already have this.
[section 1 minute example: upload]
The first example is based on the multipart upload feature.
[java]
@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);
}
}
[/java]
[section 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%%:
[java]
public void updatePicture(Profile profile, UploadedFile picture) {
File pictureOnDisk = new File();
IOUtils.copyLarge(picture.getFile(), new FileOutputStream(pictureOnDisk));
dao.atribui(pictureOnDisk, profile);
}
[/java]
[section Overriding upload settings]
You can also change the default upload settings overriding the class %%MultipartConfig%%. In example below, the size limit of upload is changed.
[java]@Component
@ApplicationScoped
public class CustomMultipartConfig extends DefaultMultipartConfig {
public long getSizeLimit() {
return 50 * 1024 * 1024; // 50MB
}
}[/java]
[section Changes in form]
You need to add the parameter %%enctype%% with %%multipart/form-data%% value. Without this attribute, the browser cannot upload files:
[xml]<form action="minha-action" enctype="multipart/form-data">[/xml]
[section Validating upload]
When the maximum size for uploaded file exceeds the configured value, VRaptor add a message on the Validator object.