private static final String CONTENT_TYPE = "text/html;charset=UTF-8";
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
request.setCharacterEncoding("UTF-8");
String path = request.getRealPath("upload/newImage");
File file = new File(path);
if (!file.exists()) {
file.mkdir();
} else {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(new File(path));
factory.setSizeThreshold(1024 * 1024);
ServletFileUpload sfu = new ServletFileUpload(factory);
try {
List<FileItem> _cache=sfu.parseRequest(request);
for(FileItem item :_cache) {
if(!item.isFormField()) {
String name=item.getFieldName();
String filedValue=item.getName();
int start=filedValue.lastIndexOf("\\");
String value=filedValue.substring(start+1);
request.setAttribute(name, value);
OutputStream os=new FileOutputStream(new File(path,value));
InputStream iis=item.getInputStream();
BufferedImage bi=ImageIO.read(iis);
int width=bi.getWidth();
int height=bi.getHeight();
System.out.println("width="+width);
System.out.println("height="+height);
iis.close();
InputStream is=item.getInputStream();
byte[] buffer=new byte[500];
int length=0;
while((length=is.read(buffer))!=-1) {
os.write(buffer, 0, length);
}
os.close();
is.close();
}
}
request.getRequestDispatcher("uploadImageSuccess.jsp").forward(request, response);
} catch (FileUploadException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
(个人微信号)
(技术公众号)
java获取上传图片长宽
最新推荐文章于 2024-07-03 02:38:38 发布