code073.java
package pack02;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
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;
import org.apache.catalina.core.ApplicationPart;
//�ļ��ϴ���servlet��
@WebServlet("/page04/code073") //����ʡ��д������ʵ��ʾurl-pattern
@MultipartConfig(location = "d:/") //���·������ʱ�ļ��еĵط���Ҳ�����ļ����ϴ��������Ȼ�����ƶ�������Ҫ�����·����
public class code073 extends HttpServlet
{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html; charset=utf-8");
PrintWriter writer1 = response.getWriter();
String path = this.getServletContext().getRealPath("/");
Part part1 = request.getPart("file1");
if (part1.getContentType().contains("image"))
{
ApplicationPart ap = (ApplicationPart)part1;
String fname1 = part1.getSubmittedFileName();
int index = fname1.lastIndexOf(".");
String fname2 = fname1.substring(index,fname1.length()); //�ļ���
String linePath = path +UUID.randomUUID().toString()+ fname2;
part1.write(linePath);
writer1.write("�ļ��ϴ��ɹ���<br>" + linePath);
}
else
{
writer1.write("��ѡ��ͼƬ�ļ���");
}
}
}