📄 imagesrv.java
字号:
/*******************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
package dndapplet.server;
import java.io.*;
import java.util.*;
import java.util.zip.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* This is the servlet which supports the drag and drop enabled file
* upload applet. This servlet has three major functions:
*
* 1. Serve a list of all the images which have been uploaded
* 2. Serve individual images when requested.
* 3. Handle the uploading of new images to the server.
*
*/
public class ImageSrv extends HttpServlet
{
// This is the default content type for an HTML page.
private static final String CONTENT_TYPE = "text/html";
/**
* The doGet method will handle requests for information from our servlet.
* This method will return a list of all the images available as well as
* returning individual images upon request.
*
* @param request the HTTP request object
* @param response the HTTP response object
*
* @exception ServletException
* @exception IOException
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
/*
* If one of the parameters is the name of an image we will return that
* specific image.
*/
if (request.getParameter("image") != null) {
doGetImage(request, response);
return;
}
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
File dir = new File("webapps/dnddemo/images");
dir.mkdirs();
/*
* We are generating a simple HTML file here. This file will have a list
* of links to each file which has been uploaded and a link to upload more
* files.
*/
out.println("<html><head><title>ImageSrv</title></head><body>");
out.println("<p>Your images</p>");
out.println("<ul>");
File files[] = dir.listFiles();
for (int i = 0; i < files.length; i++) {
out.println("<li><a href=\"?image=" + files[i].getName() + "\">" + files[i].getName() + "</a></li>");
}
out.println("</ul>");
out.println("<br /><br /><a href=\"/dnddemo/imageadd.htm\">Add new images</a>");
out.println("</body></html>");
}
/**
* This is a specialized version of the doGet method which will return the
* content of just one image or file. This method has built-in mimetype
* support for JPG, GIF, and PNG.
*
* @param request the HTTP request object
* @param response the HTTP response object
*
* @exception ServletException
* @exception IOException
*/
public void doGetImage(HttpServletRequest request, HttpServletResponse response)
throws ServletException,
IOException
{
/*
* Web browsers use the mime type or content type to determine how to handle
* a file coming from the server. If the content type indicates that a file
* is an image it will be displayed in the browser. If the content type
* indicates that a file is a ZIP archive you will probably be prompted to
* save the file.
*/
String image = request.getParameter("image");
if (image.endsWith(".jpg")) {
response.setContentType("image/jpeg");
} else if (image.endsWith(".gif")) {
response.setContentType("image/gif");
} else if (image.endsWith(".png")) {
response.setContentType("image/png");
} else {
/*
* This method supports JPG, GIF, and PNG. For all other files
* we will just return application/binary. This is a type which
* indicates that this is a generic binary file. We could extend
* this list to support many file types, but that is beyond the
* scope of this sample application.
*/
response.setContentType("application/binary");
}
readFile(image, response.getOutputStream());
}
/**
* This is a helper method for reading files from the file system and writing
* them out to the browser.
*
* @param fileName the name of the file to read
* @param out the output stream to write the file to
*
* @exception IOException
*/
private static void readFile(String fileName, OutputStream out)
throws IOException
{
File dir = new File("webapps/dnddemo/images");
FileInputStream in = new FileInputStream(new File(dir, fileName));
try {
int read;
byte[] buf = new byte[1024];
while ((read = in.read(buf)) > 0) {
out.write(buf, 0, read);
}
} finally {
if (in != null) {
in.close();
}
}
out.flush();
out.close();
}
/**
* This method handles POST requests from the browser. This method just calls
* the doPut method.
*
* @param request the HTTP request object
* @param response the HTTP response object
*
* @exception ServletException
* @exception IOException
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,
IOException
{
doPut(request, response);
}
/**
* This method handles PUT requests from the client. PUT requests will come
* from the applet portion of this application and are the way that images and
* other files can be posted to the server.
*
* @param request the HTTP request object
* @param response the HTTP response object
*
* @exception ServletException
* @exception IOException
*/
public void doPut(HttpServletRequest request, HttpServletResponse response)
throws ServletException,
IOException
{
/*
* The uploading applet will zip all the files together to create a
* faster upload and to use just one server connection.
*/
ZipInputStream in = new ZipInputStream(request.getInputStream());
/*
* This sample will write all the files to a directory on the server.
*/
File dir = new File("webapps/dnddemo/images");
dir.mkdirs();
try {
try {
while (true) {
ZipEntry entry = in.getNextEntry();
if (entry == null) {
break;
}
File f = new File(dir, entry.getName());
FileOutputStream out = new FileOutputStream(f);
try {
int read;
byte[] buf = new byte[1024];
while ((read = in.read(buf)) > 0) {
out.write(buf, 0, read);
}
} finally {
if (out != null) {
out.close();
}
}
}
} catch (ZipException ze) {
/*
* We want to catch each sip exception separately because
* there is a possibility that we can read more files from
* the archive even if one of them is corrupted.
*/
ze.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
/*
* Now that we have finished uploading the files
* we will send a reponse to the server indicating
* our success.
*/
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html><head><title>ImageSrv</title></head></html>");
out.flush();
out.close();
response.setStatus(HttpServletResponse.SC_OK);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -