/** * Appends {@link jodd.http.up.Uploadable} to buffer. */ public Buffer append(final Uploadable uploadable) { list.add(uploadable); size += uploadable.getSize(); last = null; return this; }
/** * Wraps non-Strings form values with {@link jodd.http.up.Uploadable uploadable content}. * Detects invalid types and throws an exception. So all uploadable values * are of the same type. */ protected Object wrapFormValue(final Object value) { if (value == null) { return null; } if (value instanceof CharSequence) { return value.toString(); } if (value instanceof Number) { return value.toString(); } if (value instanceof Boolean) { return value.toString(); } if (value instanceof File) { return new FileUploadable((File) value); } if (value instanceof byte[]) { return new ByteArrayUploadable((byte[]) value, null); } if (value instanceof Uploadable) { return value; } throw new HttpException("Unsupported value type: " + value.getClass().getName()); }
/** * Writes content to the writer. */ public void writeTo(final Writer writer) throws IOException { for (Object o : list) { if (o instanceof FastByteBuffer) { FastByteBuffer fastByteBuffer = (FastByteBuffer) o; byte[] array = fastByteBuffer.toArray(); writer.write(new String(array, StringPool.ISO_8859_1)); } else if (o instanceof Uploadable) { Uploadable uploadable = (Uploadable) o; InputStream inputStream = uploadable.openInputStream(); try { StreamUtil.copy(inputStream, writer, StringPool.ISO_8859_1); } finally { StreamUtil.close(inputStream); } } } }
Uploadable uploadable = (Uploadable) o; InputStream inputStream = uploadable.openInputStream(); int remaining = uploadable.getSize();
Uploadable uploadable = (Uploadable) value; String fileName = uploadable.getFileName(); if (fileName == null) { fileName = name; buffer.append("\"; filename=\"").append(fileName).append('"').append(CRLF); String mimeType = uploadable.getMimeType(); if (mimeType == null) { mimeType = MimeTypes.getMimeType(FileNameUtil.getExtension(fileName));
@Test void testUploadWithUploadable() throws IOException { HttpResponse response = HttpRequest .post("http://localhost:8173/echo2") .multipart(true) .form("id", "12") .form("file", new ByteArrayUploadable( "upload тест".getBytes(StringPool.UTF_8), "d ст", MimeTypes.MIME_TEXT_PLAIN)) .send(); assertEquals(200, response.statusCode()); assertEquals("OK", response.statusPhrase()); assertEquals("12", Data.ref.params.get("id")); assertEquals("upload тест", Data.ref.parts.get("file")); assertEquals("d ст", Data.ref.fileNames.get("file")); }
/** * Wraps non-Strings form values with {@link jodd.http.up.Uploadable uploadable content}. * Detects invalid types and throws an exception. So all uploadable values * are of the same type. */ protected Object wrapFormValue(final Object value) { if (value == null) { return null; } if (value instanceof CharSequence) { return value.toString(); } if (value instanceof Number) { return value.toString(); } if (value instanceof Boolean) { return value.toString(); } if (value instanceof File) { return new FileUploadable((File) value); } if (value instanceof byte[]) { return new ByteArrayUploadable((byte[]) value, null); } if (value instanceof Uploadable) { return value; } throw new HttpException("Unsupported value type: " + value.getClass().getName()); }
Uploadable uploadable = (Uploadable) o; InputStream inputStream = uploadable.openInputStream(); int remaining = uploadable.getSize();
Uploadable uploadable = (Uploadable) value; String fileName = uploadable.getFileName(); if (fileName == null) { fileName = name; buffer.append("\"; filename=\"").append(fileName).append('"').append(CRLF); String mimeType = uploadable.getMimeType(); if (mimeType == null) { mimeType = MimeTypes.getMimeType(FileNameUtil.getExtension(fileName));
@Test void testUploadWithUploadable() throws IOException { EchoTestServer echoTestServer = new EchoTestServer(); HttpResponse response = HttpRequest .post("http://localhost:8081/hello") .multipart(true) .form("id", "12") .form("file", new ByteArrayUploadable( "upload тест".getBytes(StringPool.UTF_8), "d ст", MimeTypes.MIME_TEXT_PLAIN)) .send(); assertEquals(200, response.statusCode()); assertEquals("OK", response.statusPhrase()); assertEquals("POST", echoTestServer.method); assertEquals("12", echoTestServer.params.get("id")); File uploadedFile = new File(echoTestServer.files.get("file").toString()); assertNotNull(uploadedFile); assertEquals("upload тест", FileUtil.readString(uploadedFile)); assertEquals("POST /hello", response.body()); echoTestServer.stop(); }
/** * Writes content to the output stream. */ public void writeTo(final OutputStream out) throws IOException { for (Object o : list) { if (o instanceof FastByteBuffer) { FastByteBuffer fastByteBuffer = (FastByteBuffer) o; out.write(fastByteBuffer.toArray()); } else if (o instanceof Uploadable) { Uploadable uploadable = (Uploadable) o; InputStream inputStream = uploadable.openInputStream(); try { StreamUtil.copy(inputStream, out); } finally { StreamUtil.close(inputStream); } } } }
/** * Appends {@link jodd.http.up.Uploadable} to buffer. */ public Buffer append(final Uploadable uploadable) { list.add(uploadable); size += uploadable.getSize(); last = null; return this; }
/** * Writes content to the output stream. */ public void writeTo(final OutputStream out) throws IOException { for (Object o : list) { if (o instanceof FastByteBuffer) { FastByteBuffer fastByteBuffer = (FastByteBuffer) o; out.write(fastByteBuffer.toArray()); } else if (o instanceof Uploadable) { Uploadable uploadable = (Uploadable) o; InputStream inputStream = uploadable.openInputStream(); try { StreamUtil.copy(inputStream, out); } finally { StreamUtil.close(inputStream); } } } }
/** * Writes content to the writer. */ public void writeTo(final Writer writer) throws IOException { for (Object o : list) { if (o instanceof FastByteBuffer) { FastByteBuffer fastByteBuffer = (FastByteBuffer) o; byte[] array = fastByteBuffer.toArray(); writer.write(new String(array, StringPool.ISO_8859_1)); } else if (o instanceof Uploadable) { Uploadable uploadable = (Uploadable) o; InputStream inputStream = uploadable.openInputStream(); try { StreamUtil.copy(inputStream, writer, StringPool.ISO_8859_1); } finally { StreamUtil.close(inputStream); } } } }