while (iter.hasNext()) {
final FileItemStream item = iter.next();
final String name = item.getFieldName();
- final String filename = item.getName();
+ // Should report only the filename even if the browser sends the
+ // path
+ final String filename = removePath(item.getName());
final String mimeType = item.getContentType();
final InputStream stream = item.openStream();
if (item.isFormField()) {
sendUploadResponse(request, response);
}
+ /**
+ * Removes any possible path information from the filename and returns the
+ * filename. Separators / and \\ are used.
+ *
+ * @param name
+ * @return
+ */
+ private static String removePath(String filename) {
+ if (filename != null) {
+ filename = filename.replaceAll("^.*[/\\\\]", "");
+ }
+
+ return filename;
+ }
+
/**
* TODO document
*
--- /dev/null
+package com.vaadin.tests.components.upload;
+
+import java.io.ByteArrayOutputStream;
+import java.io.OutputStream;
+
+import com.vaadin.tests.components.TestBase;
+import com.vaadin.ui.Label;
+import com.vaadin.ui.Upload;
+import com.vaadin.ui.Upload.FinishedEvent;
+import com.vaadin.ui.Upload.Receiver;
+
+public class TestUploadFilename extends TestBase {
+
+ private Label result = new Label("Waiting for upload");
+ private UploadReceiver receiver = new UploadReceiver();
+
+ @Override
+ protected void setup() {
+
+ Upload upload = new Upload("Upload a file", receiver);
+
+ addComponent(upload);
+ addComponent(result);
+
+ upload.addListener(new Upload.FinishedListener() {
+ public void uploadFinished(FinishedEvent event) {
+ result.setValue("Got file (should not contain path): "
+ + receiver.getFilename());
+ }
+ });
+
+ }
+
+ public static class UploadReceiver implements Receiver {
+
+ private String filename;
+
+ public OutputStream receiveUpload(String filename, String MIMEType) {
+ this.filename = filename;
+ return new ByteArrayOutputStream();
+ }
+
+ public String getFilename() {
+ return filename;
+ }
+
+ }
+
+ @Override
+ protected String getDescription() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ protected Integer getTicketNumber() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+}