I am using the following code on the client side to upload to the server
public class UploaderExample{
private static final String Boundary = "--7d021a37605f0";
public void upload(URL url, List<File> files) throws Exception
{
HttpURLConnection theUrlConnection = (HttpURLConnection) url.openConnection();
theUrlConnection.setDoOutput(true);
theUrlConnection.setDoInput(true);
theUrlConnection.setUseCaches(false);
theUrlConnection.setChunkedStreamingMode(1024);
theUrlConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary="
+ Boundary);
DataOutputStream httpOut = new DataOutputStream(theUrlConnection.getOutputStream());
for (int i = 0; i < files.size(); i++)
{
File f = files.get(i);
String str = "--" + Boundary + "\r\n"
+ "Content-Disposition: form-data;name=\"file" + i + "\"; filename=\"" + f.getName() + "\"\r\n"
+ "Content-Type: image/png\r\n"
+ "\r\n";
httpOut.write(str.getBytes());
FileInputStream uploadFileReader = new FileInputStream(f);
int numBytesToRead = 1024;
int availableBytesToRead;
while ((availableBytesToRead = uploadFileReader.available()) > 0)
{
byte[] bufferBytesRead;
bufferBytesRead = availableBytesToRead >= numBytesToRead ? new byte[numBytesToRead]
: new byte[availableBytesToRead];
uploadFileReader.read(bufferBytesRead);
httpOut.write(bufferBytesRead);
httpOut.flush();
}
httpOut.write(("--" + Boundary + "--\r\n").getBytes());
}
httpOut.write(("--" + Boundary + "--\r\n").getBytes());
httpOut.flush();
httpOut.close();
// read & parse the response
InputStream is = theUrlConnection.getInputStream();
StringBuilder response = new StringBuilder();
byte[] respBuffer = new byte[4096];
while (is.read(respBuffer) >= 0)
{
response.append(new String(respBuffer).trim());
}
is.close();
System.out.println(response.toString());
}
public static void main(String[] args) throws Exception
{
List<File> list = new ArrayList<File>();
list.add(new File("C:\\square.png"));
list.add(new File("C:\\narrow.png"));
UploaderExample uploader = new UploaderExample();
uploader.upload(new URL("http://systemout.com/upload.php"), list);
}
}
I have tried writing the servlet that receives the image file and saves it to a folder on the server....but have failed miserably...This is part of an academic project i need to submit as part of my degree....Please Help!!!
I want help ...can someone guide me on how the servlet will be written....
I tried the following:
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
InputStream input = null;
OutputStream output = null;
try {
input = request.getInputStream();
output = new FileOutputStream("C:\\temp\\file.png");
byte[] buffer = new byte[10240];
for (int length = 0; (length = input.read(buffer)) > 0 ; ) {
output.write(buffer, 0, length);
}
}
catch(Exception e){
out.println(e.getMessage());
}
finally {
if (output != null) {
output.close();
}
if (input != null) {
input.close();
}
}
out.println("Success");
}
catch(Exception e){
out.println(e.getMessage());
}
finally {
out.close();
}
}
I went ahead and tried the fileupload from apache.org....and wrote the following servlet code:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println(1);
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart) {
// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Parse the request
List /* FileItem */ items = upload.parseRequest(request);
// Process the uploaded items
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
//processFormField(item);
} else {
//processUploadedFile(item);
String fieldName = item.getFieldName();
String fileName = item.getName();
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
//write to file
File uploadedFile = new File("C:\\temp\\image.png");
item.write(uploadedFile);
out.println("Sucess!");
}
}
} else {
out.println("Invalid Content!");
}
} catch (Exception e) {
out.println(e.getMessage());
} finally {
out.close();
}
}
However i am still confused on how to write the multipart code on the client side...the one i posted above is not working with my servlet implementation.....help please....some links where i can learn writing posting multipart form from java desktop app would be useful
So here's my recommendation: don't write this code yourself! Use http://commons.apache.org/fileupload/ instead. It will save you a lot of headaches, and you'll be up and running quite quickly. I'm pretty sure that problem is that the InputStream contains the multi-part boundaries, and is thus not a valid image.
Here's another observation: since you're not doing any transformations on the image, there's no need to read and write the image bytes using ImageIO. You're better off writing the bytes straight from the InputStream to the file.
Related
i wrote my code as below for download files:
String filename = request.getParameter("file").toString();
String filepath = request.getParameter("path").toString(); ;
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");
FileInputStream fileInputStream=new FileInputStream(filepath + filename);
int i;
while ((i = fileInputStream.read()) != -1) {
out.write(i);
}
fileInputStream.close();
i was using this code in jsp page with scriplet tags ie <% %>
Now i tried it in servlet and now able to open the .xlsx file. the working code is as below:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String filename = request.getParameter("file").toString();
String filepath = request.getParameter("path").toString();
File f = new File (filepath + filename);
//set the content type(can be excel/word/powerpoint etc..)
String type = request.getParameter("type");
response.setContentType (type);
//set the header and also the Name by which user will be prompted to save
response.setHeader ("Content-Disposition", "attachment; filename=\""+filename+"\"");
//get the file name
String name = f.getName().substring(f.getName().lastIndexOf("/") + 1, f.getName().length());
//OPen an input stream to the file and post the file contents thru the
//servlet output stream to the client m/c
InputStream in = new FileInputStream(f);
try{
ServletOutputStream outs = response.getOutputStream();
int bytesRead;
byte[] buf = new byte[4 * 1024]; // 4K buffer
try {
while ((bytesRead = in.read(buf)) != -1){
outs.write(buf, 0, bytesRead);
}
} catch (IOException ioe) {
ioe.printStackTrace(System.out);
}
outs.flush();
outs.close();
in.close();
}catch(Exception e){
e.printStackTrace();
}
}
I am trying to create a program that will download resources from a webpage into a file. I created an mkir function that creates a directory whose name is a hexadecimal version of the hash of a given String. I then, created a saveResource function that saves a resource in a file, as well as in a byte array. However, when I try saving the resource into the file I get an error message stating: java.io.FileNotFoundException: 648451a1 (Is a directory)
Here are the functions:
public static File mkdir(String s) throws IOException
{
String dirname = s;
s = Integer.toHexString(dirname.hashCode());
File directory = new File(s);
if (!directory.exists() && !directory.mkdir())
throw new IOException("can't make directory for " + s);
return directory;
}
public static byte[] saveResource(File dir, String urlString,
String argURLString)
throws IOException, URISyntaxException
{
URL u = new URL(urlString);
URLConnection uc = u.openConnection();
urlString = uc.getContentType();
int contentLength = uc.getContentLength();
try (InputStream raw = uc.getInputStream()) {
InputStream in = new BufferedInputStream(raw);
byte[] data = new byte[contentLength];
int offset = 0;
while (offset < contentLength) {
int bytesRead = in.read(data, offset, data.length - offset);
if (bytesRead == -1) break;
offset += bytesRead;
}
if (offset != contentLength) {
throw new IOException("Only read " + offset
+ " bytes; Expected " + contentLength + " bytes");
}
urlString = u.getFile();
urlString = urlString.substring(urlString.lastIndexOf('/') + 1);
try (FileOutputStream fout = new FileOutputStream(dir)) {
fout.write(data);
fout.flush();
}
return data;
}
}
The error is simple and coming because you can't write data in directory.
Try to print dir.isDirectory() to confirm if it's directory. Since its part of argument, check the caller method.
I have a problem regarding on uploading a file. When I upload the files myself(localhost) it actually works but when I let other people in the same network upload their file it gives me an error:
(The system cannot find the path specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at sources.UploadAIP.actions.uploadAction.processRequest(uploadAction.java:49)
Here is my actual code:
public class uploadAction extends AbstractAppAction
{
public boolean processRequest(HttpServlet servlet, HttpServletRequest request, HttpServletResponse response)
{
try{
Properties appProperties = getAppProperties();
String dbMap = appProperties.getProperty("dbMap");
DB db = getDBConnection(dbMap);
String myDirectory = "C:\\tmp";
String uploadedFile = request.getParameter("filename");
System.out.println("srcfile: " +myDirectory);
System.out.println("file: " +uploadedFile);
String errorMessage = "";
ServletContext sc = servlet.getServletContext();
String fileName = StringUtil.stringReplace(uploadedFile,"\\","\\");
int i = fileName.lastIndexOf("\\");
if (i > 0) {
fileName = fileName.substring(i+1);
}
File srcFile = new File(uploadedFile);
File targetDirectory = new File(myDirectory);
String dirname = StringUtil.stringReplace(targetDirectory.toString() ,"\\","\\");
System.out.println("directory name:" +dirname);
File destFile = new File(dirname+"\\"+fileName);
System.out.println(destFile);
System.out.println("here is the parent directory: " +targetDirectory);
if(!targetDirectory.exists()){
targetDirectory.mkdirs();
}
InputStream inStream;
OutputStream outStream;
try{
inStream = new FileInputStream(srcFile);
outStream = new FileOutputStream(destFile);
byte[] buffer = new byte[4096];
int length;
//copy the file content in bytes
while ((length = inStream.read(buffer)) > 0){
outStream.write(buffer, 0, length);
}
outStream.close();
}catch(Exception e){
e.printStackTrace();
}
fileName = StringUtil.stringReplace(uploadedFile, "\\", "\\");
int u = fileName.lastIndexOf("\\");
if (u > 0)
{
fileName = fileName.substring(i + 1);
}
if (!dirname.endsWith("\\"))
{
dirname = dirname + "\\";
}
File f = new File(dirname);
String uploadDir = dirname;
System.out.println("uploadDirectory" +uploadDir);
} catch (Exception ex) {
request.setAttribute("message", ex.toString());
ex.printStackTrace();
}
return (true);
}
}
Your code assumes that the file being uploaded is present on the same local machine, which isnt true, since you are receiving uploads from a local network.
This is why it works on your local machine, but not across a network.
To upload a file, you need a multipart form and a servlet that correctly handles a multipart request.
This tutorial should help you:
http://www.avajava.com/tutorials/lessons/how-do-i-upload-a-file-to-a-servlet.html
in a gwt web application.
I have to send a file and some parameter attached to it.
on ServerSide
try {
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iterator = upload.getItemIterator(request);
while (iterator.hasNext()) {
FileItemStream item = iterator.next();
if (item.isFormField()) {
String fieldName=item.getFieldName();
String fieldValue = Streams.asString(item.openStream());
System.out.println(" chk " +fieldName +" = "+ fieldValue);
} else {
stream = item.openStream();
fileName = item.getName();
mimetype = item.getContentType();
int c;
while ((c = stream.read()) != -1) {
System.out.print((char) c);
}
}
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.println("out of try");
ByteArrayOutputStream output = new ByteArrayOutputStream();
int nRead;
while ((nRead = stream.read(buffer, 0, buffer.length)) != -1) {
System.out.println("lenth111" +nRead);
output.write(buffer, 0, nRead);
}
System.out.println("lenth" +nRead);
output.flush();
with this code i can read the stream.
and also on console "out of try" is also printed
And finally on while ((nRead = stream.read(buffer, 0, buffer.length)) != -1)
line i got a Warning
WARNING: /UploadFileServlet: org.apache.commons.fileupload.FileItemStream$ItemSkippedException.
How to solve this problem.??
Answer a bit late but I had the same problem.
Why you get that exception: The JavaDocs of ItemSkippedException explain a little bit:
This exception is thrown, if an attempt is made to read data from the InputStream, which has been returned by FileItemStream.openStream(), after Iterator.hasNext() has been invoked on the iterator, which created the FileItemStream.
You are using the InputStream stream outside the while loop which causes the problem because another iteration is called which closes (skips) the file InputStream you try to read from.
Solution: Use the InputStream inside the while loop. If you need all form-fields before processing the file, ensure you set it in the right order on client side. First all fields, last the file. For example using the JavaScript FormData:
var fd = new window.FormData();
fd.append("param1", param1);
fd.append("param2", param2);
// file must be last parameter to append
fd.append("file", file);
And on server side:
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
FileItemStream item = iter.next();
InputStream stream = item.openStream();
// the order of items is given by client, first form-fields, last file stream
if (item.isFormField()) {
String name = item.getFieldName();
String value = Streams.asString(stream);
// here we get the param1 and param2
} else {
String filename = item.getName();
String mimetype = item.getContentType();
ByteArrayOutputStream output = new ByteArrayOutputStream();
int nRead;
while ((nRead = stream.read(buffer, 0, buffer.length)) != -1) {
System.out.println("lenth111" +nRead);
output.write(buffer, 0, nRead);
}
System.out.println("lenth" +nRead);
output.flush();
}
}
I had the same issue. To solve it, I had to break out of the while loop after reading the last item:
import java.io.InputStream;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
final String ID = "id";
final String FILE_NAME = "fileName";
final String APIKEY = "apikey";
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
String id = null;
String apikey = null;
String fileName = null;
InputStream stream = null;
FileItemIterator iterator = upload.getItemIterator(request);
outer: while (iterator.hasNext()) {
FileItemStream item = iterator.next();
switch (item.getFieldName()) {
case ID:
id = Streams.asString(item.openStream());
break;
case APIKEY:
apikey = Streams.asString(item.openStream());
break;
case FILE_NAME:
fileName = item.getName();
stream = item.openStream();
break outer;
}
}
break outer; is the line that solved my issue.
I don't know why I'm getting the following exception when reading a mail with an attachment file from mail server:
Exception in thread "main" javax.mail.MessagingException: Missing start boundary
at javax.mail.internet.MimeMultipart.parsebm<MimeMultipart.java:872)
at javax.mail.internet.MimeMultipart.parse<MimeMultipart.java:493)
at javax.mail.internet.MimeMultipart.getCount<MimeMultipart.java:240)
at GetParts.handleMultipart(GetParts.java:57)
at GetParts.main(GetParts.java:42)
The file which I'm using to read those messages is:
import java.io.*;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class GetParts {
public static void main (String args[])
throws Exception {
String host = args[0];
String username = args[1];
String password = args[2];
// Get session
Properties props=new Properties();
props.put("mail.mime.multipart.ignoremissingboundaryparamete",true);
Session session = Session.getInstance(
props, null);
ContentType ct=new ContentType();
// Get the store
Store store = session.getStore("pop3");
store.connect(host, username, password);
// Get folder
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
BufferedReader reader = new BufferedReader (
new InputStreamReader(System.in));
// Get directory
Message message[] = folder.getMessages();
for (int i=0, n=message.length; i<n; i++) {
System.out.println(i + ": "
+ message[i].getFrom()[0]
+ "\t" + message[i].getSubject());
//message[i].setHeader("Content-Type","multipart/mixed");
System.out.println("Do you want to get the content? [YES to read/QUIT to end]");
String line = reader.readLine();
if ("YES".equals(line)) {
Object content = message[i].getContent();
if (content instanceof Multipart) {
handleMultipart((Multipart)content);
} else {
handlePart(message[i]);
}
} else if ("QUIT".equals(line)) {
break;
}
}
// Close connection
folder.close(false);
store.close();
}
public static void handleMultipart(Multipart multipart)
throws MessagingException, IOException {
System.out.println(multipart.getCount());
for (int i=0, n=multipart.getCount(); i<n; i++) {
handlePart(multipart.getBodyPart(i));
}
}
public static void handlePart(Part part)
throws MessagingException, IOException {
String disposition = part.getDisposition();
System.out.println("Disposition "+disposition);
String contentType = part.getContentType();
System.out.println("contentType "+contentType);
if (disposition == null) { // When just body
System.out.println("Null: " + contentType);
// Check if plain
if ((contentType.length() >= 10) &&
(contentType.toLowerCase().substring(
0, 10).equals("text/plain"))) {
part.writeTo(System.out);
} else { // Don't think this will happen
System.out.println("Other body: " + contentType);
part.writeTo(System.out);
}
} else if (disposition.equalsIgnoreCase(Part.ATTACHMENT)) {
System.out.println("Attachment: " + part.getFileName() +
" : " + contentType);
saveFile(part.getFileName(), part.getInputStream());
} else if (disposition.equalsIgnoreCase(Part.INLINE)) {
System.out.println("Inline: " +
part.getFileName() +
" : " + contentType);
saveFile(part.getFileName(), part.getInputStream());
} else { // Should never happen
System.out.println("Other: " + disposition);
}
}
public static void saveFile(String filename,
InputStream input) throws IOException {
if (filename == null) {
filename = File.createTempFile("xx", ".out").getName();
}
// Do no overwrite existing file
File file = new File(filename);
for (int i=0; file.exists(); i++) {
file = new File(filename+i);
}
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
BufferedInputStream bis = new BufferedInputStream(input);
int aByte;
while ((aByte = bis.read()) != -1) {
bos.write(aByte);
}
bos.flush();
bos.close();
bis.close();
}
}
I've just had the same problem. The boundary is specified within the Multipart Content-Type. You can find further information in this source. You can also watch the one of your current Message using the getContentType() function. In my case I obtained this result:
multipart/mixed; boundary=--boundary_25_2d74d02b-d0d6-4f28-a311-4d1b7d107417
So the getCount() function uses this boundary to separate all the parts that compose the multiple part. Looks like there could be cases in which this boundary is corrupted.
The mail.mime.multipart.ignoreexistingboundaryparameter System property may be set to true to cause any boundary to be ignored and instead search for a boundary line in the message as with mail.mime.multipart.ignoremissingboundaryparameter.
I followed this instructions and everything works all right. I added the code below:
System.setProperty("mail.mime.multipart.ignoreexistingboundaryparameter", "true");
Hope it helps!
try to set mode on the multipartEntityBuilder
ex:
multipartEntityBuilder.setMode(HttpMultipartMode.RFC6532);