上传图片
------
使用apache的上传附件插件:commons-fileupload.jar
commons-io.jar
A:创建页面:upload.jsp
B:编写servlet:PicUploadServlet.java
c:配置web.xml
jsp
-----------
<!-- 上传附件的表单(二进制数据)注意两点:
1、post请求,
2、enctype="multipart/form-data" -->
<form name="myForm" action="${pageContext.request.contextPath }/picUpload"
method="post" enctype="multipart/form-data">
username:<input type="text" name="username"><br/>
file:<input type="file" name="file" />
<input type="submit" value="上传">
</form>
servlet
---------
public class PicUploadServlet extends HttpServlet
{
private File tempPath = null;
private File uploadPath = null;
//初始化文件上传的目录
//1、存储图片的临时路径
//2、存放(上传)文件的真实路径
@Override
public void init()
throws ServletException
{
//获取临时交换目录
tempPath = new File(this.getServletContext().getRealPath("temp"));
System.out.println(this.getServletContext().getRealPath("temp"));
if(!tempPath.exists())
{
tempPath.mkdir();
}
//获取上传文件的真实路径
uploadPath = new File(this.getServletContext().getRealPath("upload/images"));
if(!uploadPath.exists())
{
//创建多级目录
uploadPath.mkdirs();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
DiskFileItemFactory factory = new DiskFileItemFactory();
//设置文件上传的大小块4k
factory.setSizeThreshold(4096);
factory.setRepository(tempPath);
//创建上传组件
ServletFileUpload upload = new ServletFileUpload(factory);
//设置文件上传大小 4M
upload.setSizeMax(4096000);
//得到表单元素列表
try
{
List fileItems = upload.parseRequest(req);
//遍历该集合
Iterator fileItem = fileItems.iterator();
String username = "";
while(fileItem.hasNext())
{
FileItem item = (FileItem)fileItem.next();
//判断该元素是否为普通表单元素
if(item.isFormField())
{
if("username".equals(item.getFieldName()))
{
username = item.getString("utf-8");
System.out.println(username);
}
}
//附件上传元素
else
{
String fileName = item.getName();
//获取后缀名
String ext = fileName.substring(fileName.lastIndexOf(".")+1, fileName.length());
//得到一个不会重复的文件名
Calendar c = Calendar.getInstance();
fileName = String.valueOf(c.getTimeInMillis());
//fileName = c.getTimeInMillis()+"";
//拼接一个带不会重复的文件名的全路径
String fullFilePath = uploadPath+"/"+fileName+"."+ext;
//将缓存中的二进制流的附件,写入该路径(附件存放在磁盘下的具体路径)
File f = new File(fullFilePath);
item.write(f);
//将上面的原图同时生成一张等比例缩放的缩略图
//创建一个缩略图的全路径
String newurl = uploadPath+"/"+fileName+"_min."+ext;
//构建image对象
Image src = ImageIO.read(f);
//设置目标大小
float tagSize = 200;
int oldWidth = src.getWidth(null);
int oldHeight = src.getHeight(null);
//定义新的高度和宽度
int newWidth=0;
int newHeight=0;
//声明临时大小
float tempDouble=0;
//横放
if(oldWidth > oldHeight)
{
tempDouble = oldWidth/tagSize;
}
else
{
tempDouble = oldHeight/tagSize;
}
//得到新的高度和宽度
newWidth = Math.round(oldWidth/tempDouble);
newHeight = Math.round(oldHeight/tempDouble);
//创建新图对象
BufferedImage tag = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0,newWidth,newHeight,null);
FileOutputStream newImage = new FileOutputStream(newurl);
//创建编码对象
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newImage);
//JPEG编码
encoder.encode(tag);
//关闭流
newImage.close();
}
}
req.getRequestDispatcher("success.jsp").forward(req, resp);
}
catch (FileUploadException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
上传图片功能源码
最新推荐文章于 2023-01-05 18:28:34 发布