commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bcub...@MIT.EDU
Subject Newbie, using the File Upload library--> to output a text file
Date Thu, 28 Aug 2008 21:21:43 GMT
I understand very well how to read an input file and output the carbon copy
using response.getOutputStream().println(new String(uploadItem.get()));
where upload is FileItem uploadItem = getFileItem(request);. Now I want to do
something a little different. Reading an input file and output a file based
upon the contents of the input file which uses the content of the input file to
write the output file. The method private  getStringOutput(String input)
takes an input, i.e. the contents of the input file and produce an output file
called "output.txt" and it returns return outputFileName which is "output.txt".
Now my question is response.getOutputStream().println("output.txt");
outputing just the filename or its contents. I want to ouput the contents of the
output.txt, let me know how to do that?
Your attention to this matter is highly appreciated.
Best Regards,
Barry Barrios



Below is the complete code of the servlet:




package de.herbstcampus.server;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;




public class FileUploadServlet extends HttpServlet {
	private static final long serialVersionUID = 1156467149259077140L;

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {


		FileItem uploadItem = getFileItem(request);
		String output = getStringOutput(new String(uploadItem.get()));
		/*
		 * Note this must be 'text/html', otherwise the onSubmitComplete(...)
		 * won't work properly and the browser may open a save dialog.
		 */
		response.setContentType("text/html");


		if (uploadItem == null) {
			response.getWriter().write("No data");
			return;
		} else {
			response.getOutputStream().println(output);
		}
	}

	@SuppressWarnings("unchecked")
	private FileItem getFileItem(HttpServletRequest request) {
		FileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(factory);

		try {
			List<FileItem> items = upload.parseRequest(request);

			for (FileItem item: items) {
				if (!item.isFormField()
						&& "uploadFormElement".equals(item.getFieldName())) {
					return item;
				}
			}
		} catch (FileUploadException e) {
			return null;
		}

		return null;
	}
	private String getStringOutput(String input){
		String outputFileName="output.txt";
		CVRPOutputs outputs; // this object manages the outputs file
		outputs = new CVRPOutputs(outputFileName,input);
	        outputs.printOutputFile();
	        return outputFileName;

	}

}

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message