java在页面上 验证上传的图片的尺寸 <input type="file" id="img1" name="img1" > 10
4个回答
展开全部
<input type="text" size="4" value="10" name="fileSizeLimit" id="fileSizeLimit"/> K
<input type="file" name="file1" id="file1" size="40" onchange="changeSrc(this)"/>
<img src="about:blank" id="fileChecker" alt="test" height="18"/>
//前面为网页
<script type="text/javascript">
var oFileChecker = document.getElementById("fileChecker");
function changeSrc(filePicker)
{
oFileChecker.src = filePicker.value;//读图片
}
//这个很重要,判断是否完全读完,否则判断图片大小不准确
oFileChecker.onreadystatechange = function ()
{
if (oFileChecker.readyState == "complete")
{
checkSize();
}
}
function checkSize()
{
var limit = document.getElementById("fileSizeLimit").value * 1024;
if (oFileChecker.fileSize > limit)
{
alert("too large");
}
else
{
alert("ok");
}
}
</script>
<input type="file" name="file1" id="file1" size="40" onchange="changeSrc(this)"/>
<img src="about:blank" id="fileChecker" alt="test" height="18"/>
//前面为网页
<script type="text/javascript">
var oFileChecker = document.getElementById("fileChecker");
function changeSrc(filePicker)
{
oFileChecker.src = filePicker.value;//读图片
}
//这个很重要,判断是否完全读完,否则判断图片大小不准确
oFileChecker.onreadystatechange = function ()
{
if (oFileChecker.readyState == "complete")
{
checkSize();
}
}
function checkSize()
{
var limit = document.getElementById("fileSizeLimit").value * 1024;
if (oFileChecker.fileSize > limit)
{
alert("too large");
}
else
{
alert("ok");
}
}
</script>
展开全部
<HTML>
<HEAD>
<SCRIPT>
function getImageDimension (imgURL, loadHandler) {
var img = new Image();
img.onload = loadHandler;
if (document.layers
&& location.protocol.toLowerCase() != 'file:'
&& navigator.javaEnabled())
netscape.security.PrivilegeManager.enablePrivilege(
'UniversalFileRead'
);
img.src = imgURL;
}
function showImageDimensions () {
alert(this.width + 'x' + this.height);
}
</SCRIPT>
<SCRIPT>
function checkImageDimensions (fileName) {
var imgURL = 'file:///' + fileName;
getImageDimension(imgURL, showImageDimensions);
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="formName">
<INPUT TYPE="file" NAME="fileName">
<BR><INPUT TYPE="button" VALUE="check image dimensions"
ONCLICK="checkImageDimensions(this.form.fileName.value)"
>
</FORM>
</BODY>
</HTML>
<HEAD>
<SCRIPT>
function getImageDimension (imgURL, loadHandler) {
var img = new Image();
img.onload = loadHandler;
if (document.layers
&& location.protocol.toLowerCase() != 'file:'
&& navigator.javaEnabled())
netscape.security.PrivilegeManager.enablePrivilege(
'UniversalFileRead'
);
img.src = imgURL;
}
function showImageDimensions () {
alert(this.width + 'x' + this.height);
}
</SCRIPT>
<SCRIPT>
function checkImageDimensions (fileName) {
var imgURL = 'file:///' + fileName;
getImageDimension(imgURL, showImageDimensions);
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="formName">
<INPUT TYPE="file" NAME="fileName">
<BR><INPUT TYPE="button" VALUE="check image dimensions"
ONCLICK="checkImageDimensions(this.form.fileName.value)"
>
</FORM>
</BODY>
</HTML>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
java上传可以使用common-fileupload上传组件的。
common-fileupload是jakarta项目组开发的一个功能很强大的上传文件组件
下面先介绍上传文件到服务器(多文件上传):
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.util.regex.*;
import org.apache.commons.fileupload.*;
public class upload extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GB2312";
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out=response.getWriter();
try {
DiskFileUpload fu = new DiskFileUpload();
// 设置允许用户上传文件大小,单位:字节,这里设为2m
fu.setSizeMax(2*1024*1024);
// 设置最多只允许在内存中存储的数据,单位:字节
fu.setSizeThreshold(4096);
// 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
fu.setRepositoryPath("c:\\windows\\temp");
//开始读取上传信息
List fileItems = fu.parseRequest(request);
// 依次处理每个上传的文件
Iterator iter = fileItems.iterator();
//正则匹配,过滤路径取文件名
String regExp=".+\\\\(.+)$";
//过滤掉的文件类型
String[] errorType={".exe",".com",".cgi",".asp"};
Pattern p = Pattern.compile(regExp);
while (iter.hasNext()) {
FileItem item = (FileItem)iter.next();
//忽略其他不是文件域的所有表单信息
if (!item.isFormField()) {
String name = item.getName();
long size = item.getSize();
if((name==null||name.equals("")) && size==0)
continue;
Matcher m = p.matcher(name);
boolean result = m.find();
if (result){
for (int temp=0;temp if (m.group(1).endsWith(errorType[temp])){
throw new IOException(name+": wrong type");
}
}
try{
//保存上传的文件到指定的目录
//在下文中上传文件至数据库时,将对这里改写
item.write(new File("d:\\" + m.group(1)));
out.print(name+" "+size+"
");
}
catch(Exception e){
out.println(e);
}
}
else
{
throw new IOException("fail to upload");
}
}
}
}
catch (IOException e){
out.println(e);
}
catch (FileUploadException e){
out.println(e);
}
}
}
common-fileupload是jakarta项目组开发的一个功能很强大的上传文件组件
下面先介绍上传文件到服务器(多文件上传):
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.util.regex.*;
import org.apache.commons.fileupload.*;
public class upload extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GB2312";
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out=response.getWriter();
try {
DiskFileUpload fu = new DiskFileUpload();
// 设置允许用户上传文件大小,单位:字节,这里设为2m
fu.setSizeMax(2*1024*1024);
// 设置最多只允许在内存中存储的数据,单位:字节
fu.setSizeThreshold(4096);
// 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
fu.setRepositoryPath("c:\\windows\\temp");
//开始读取上传信息
List fileItems = fu.parseRequest(request);
// 依次处理每个上传的文件
Iterator iter = fileItems.iterator();
//正则匹配,过滤路径取文件名
String regExp=".+\\\\(.+)$";
//过滤掉的文件类型
String[] errorType={".exe",".com",".cgi",".asp"};
Pattern p = Pattern.compile(regExp);
while (iter.hasNext()) {
FileItem item = (FileItem)iter.next();
//忽略其他不是文件域的所有表单信息
if (!item.isFormField()) {
String name = item.getName();
long size = item.getSize();
if((name==null||name.equals("")) && size==0)
continue;
Matcher m = p.matcher(name);
boolean result = m.find();
if (result){
for (int temp=0;temp if (m.group(1).endsWith(errorType[temp])){
throw new IOException(name+": wrong type");
}
}
try{
//保存上传的文件到指定的目录
//在下文中上传文件至数据库时,将对这里改写
item.write(new File("d:\\" + m.group(1)));
out.print(name+" "+size+"
");
}
catch(Exception e){
out.println(e);
}
}
else
{
throw new IOException("fail to upload");
}
}
}
}
catch (IOException e){
out.println(e);
}
catch (FileUploadException e){
out.println(e);
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个用JS来判断。。。正则表达式来做
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询