aa

Upload Filepackage khannedy.web.servlet;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

@WebServlet(name = “UploadFile”, urlPatterns = {“/UploadFile”})
public class UploadFileServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

Part part = req.getPart(“upload”);

String temp = part.getHeader(“content-disposition”).split(“;”)[2].trim();
String filename = temp.substring(“filename=\””.length(), temp.length() – 1);

InputStream inputStream = part.getInputStream();
FileOutputStream stream = new FileOutputStream(getServletContext().getRealPath(“upload”) + “/” + filename);

int i = -1;
while ((i = inputStream.read()) != -1) {
stream.write(i);
}

stream.close();
inputStream.close();

resp.setContentType(“text/html;charset=UTF-8”);
PrintWriter out = resp.getWriter();
try {
out.println(“”);
out.println(“”);
out.println(“Servlet SayHelloServlet”);
out.println(“”);
out.println(“”);
out.println(““);
out.println(“Download Here”);
out.println(“
“);
out.println(“”);
out.println(“”);
} finally {
out.close();
}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s