为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

jsp上传图片方法

2017-10-08 18页 doc 331KB 47阅读

用户头像

is_348501

暂无简介

举报
jsp上传图片方法jsp上传图片方法 ? 博客园首页 如何在Jsp上传图片 作者:夏夏知 来源:博客园 发布时间:2012-03-28 11:43 阅读:246 次 原文链接 [收藏] 1. 新建一个Dynamic Web Project: 2. 键入工程名UploadImage: 3. 选择Dynamic web module version :2.5 4. 点击“Finish” 5. 新建两个jsp文件:ImageUpload.jsp和ImageUploadHandler.jsp to correcting mis...
jsp上传图片方法
jsp上传图片方法 ? 博客园首页 如何在Jsp上传图片 作者:夏夏知 来源:博客园 发布时间:2012-03-28 11:43 阅读:246 次 原文链接 [收藏] 1. 新建一个Dynamic Web Project: 2. 键入工程名UploadImage: 3. 选择Dynamic web module version :2.5 4. 点击“Finish” 5. 新建两个jsp文件:ImageUpload.jsp和ImageUploadHandler.jsp to correcting misunderstandings advocate good cadre style. 2, to carry out the "double" of human activities. Bangkun will implement "on the in-depth development of the grassroots, the people to worry, notification on the normalization of promoting harmony activities, making the double work innovation system, the demands of the masses reflect back channels, help enterprises to solve practical problems as much as possible. 3, visit 6. 新建一个类,专门用来处理图片上传 新建一个包mytest: 在包下新建一个类FileUpload: 代码内容: package mytest; import java.awt.image.BufferedImage; import java.io.*; import java.awt.Image; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; public class FileUpload { public static void uploadImage(String imgFolderPath,String srcFileName,String dstFileName) throws IOException to correcting misunderstandings advocate good cadre style. 2, to carry out the "double" of human activities. Bangkun will implement "on the in-depth development of the grassroots, the people to worry, notification on the normalization of promoting harmony activities, making the double work innovation system, the demands of the masses reflect back channels, help enterprises to solve practical problems as much as possible. 3, visit { //判断文件夹image是否存在,若不存在则创建 File folder = new File(imgFolderPath); System.out.println("function 'uploadFileTest2'-imgFolderPath'"+imgFolderPath); if(!folder.exists()) { folder.mkdir(); System.out.println("maked a folder!"); } File _file = new File(srcFileName); //读入文件 Image src = javax.imageio.ImageIO.read(_file); //构造Image对象 int wideth=src.getWidth(null); //得到源图宽 int height=src.getHeight(null); //得到源图长 BufferedImage tag = new BufferedImage(wideth,height,BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(src,0,0,wideth,height,null); FileOutputStream out=new FileOutputStream(dstFileName); //输出到文件流 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); //JPEG编码 out.close(); System.out.println("function 'uploadFileTest2'-status:A Image File Saved!"); } } 发现报错: 找到代码的库Libraries: to correcting misunderstandings advocate good cadre style. 2, to carry out the "double" of human activities. Bangkun will implement "on the in-depth development of the grassroots, the people to worry, notification on the normalization of promoting harmony activities, making the double work innovation system, the demands of the masses reflect back channels, help enterprises to solve practical problems as much as possible. 3, visit 打开rt.jar所在文件夹: 复制这个文件: 右键粘贴到WEB_INF\lib下: 效果: 没再报错。 7. 修改ImageUpload.jsp 说明:这个jsp文件,需要一个能选择文件的控件"); String photo=request.getParameter("imgUpload");//获取file控件里的路径(绝对路径) int indexOfLine = photo.lastIndexOf("\\"); String fileName = photo.substring(indexOfLine+1,photo.length());// 文件名(含后缀,不包含路径信息) to correcting misunderstandings advocate good cadre style. 2, to carry out the "double" of human activities. Bangkun will implement "on the in-depth development of the grassroots, the people to worry, notification on the normalization of promoting harmony activities, making the double work innovation system, the demands of the masses reflect back channels, help enterprises to solve practical problems as much as possible. 3, visit String destFilePathAndName = filePath+"\\image\\"+fileName;//要 保存文件的路径 FileUpload.uploadImage(filePath+"\\image\\",photo,destFilePathAn dName);//上传图片到目的路径 String relativeFilePath = ".\\image\\"+fileName;//用来显示图片的相对路 径 out.println(photo+"
"); out.println(destFilePathAndName+"
"); %> (5)显示图片 上传的图片:image display (6)全版布局 9. 运行 选中ImageUpload.jsp这个文件,并点击“运行”三角形: 出现问题: to correcting misunderstandings advocate good cadre style. 2, to carry out the "double" of human activities. Bangkun will implement "on the in-depth development of the grassroots, the people to worry, notification on the normalization of promoting harmony activities, making the double work innovation system, the demands of the masses reflect back channels, help enterprises to solve practical problems as much as possible. 3, visit 看来一开始把版本设置成2.5无效。打开该项目所在目录: 技巧:右键项目,选择Properties: 看到Loacation: 复制这个路径,打开“我的电脑”并在地址栏上粘贴,回车: 打开.settings: 使用记事本打开这个XML文档: to correcting misunderstandings advocate good cadre style. 2, to carry out the "double" of human activities. Bangkun will implement "on the in-depth development of the grassroots, the people to worry, notification on the normalization of promoting harmony activities, making the double work innovation system, the demands of the masses reflect back channels, help enterprises to solve practical problems as much as possible. 3, visit 把“jst.web”的version改成2.5 右键点项目,刷新一下: 注:也可以安装一些插件,可以右键直接打开文件夹。 10. 重新运行 点击OK: 选择“Manually define a new server”,并选择一个Tomcat(因示例使用Tomcat 6.0,配置时看具体版本而定) to correcting misunderstandings advocate good cadre style. 2, to carry out the "double" of human activities. Bangkun will implement "on the in-depth development of the grassroots, the people to worry, notification on the normalization of promoting harmony activities, making the double work innovation system, the demands of the masses reflect back channels, help enterprises to solve practical problems as much as possible. 3, visit 点击“Add”: 在Browse…选择安装Tomcat6.0的路径,点击“Finish”,离开这个对话框,并回到上一 层对话框,再点击“Finish”开始运行。 出现问题: 关闭这个文件,重新点击运行(保证当前打开的文件是ImageUpload.jsp): 选择一个文件: to correcting misunderstandings advocate good cadre style. 2, to carry out the "double" of human activities. Bangkun will implement "on the in-depth development of the grassroots, the people to worry, notification on the normalization of promoting harmony activities, making the double work innovation system, the demands of the masses reflect back channels, help enterprises to solve practical problems as much as possible. 3, visit 发现没有提交按钮,回去修改ImageUpload.jsp: 再次运行,运行后点击进行刷新: 点击“提交”: 删除调试信息及修改图片大小: 注释掉out: 加宽图片: 重新运行: to correcting misunderstandings advocate good cadre style. 2, to carry out the "double" of human activities. Bangkun will implement "on the in-depth development of the grassroots, the people to worry, notification on the normalization of promoting harmony activities, making the double work innovation system, the demands of the masses reflect back channels, help enterprises to solve practical problems as much as possible. 3, visit 实际上,会出现图片能在eclipse内置的浏览器中正常显示,但却不能在外部浏览器中显示,原因在于,显示图片时用的文件必须是已经上传到服务器的图片,而不能是服务端的本地文件,而客户端是不允许直接访问服务器的本地文件,所以导致了错误,因此,在浏览器中检验一下: 复制选中的URL: 打开IE浏览器,在地址栏粘贴并回车: 选择一个文件: 点击“提交”: to correcting misunderstandings advocate good cadre style. 2, to carry out the "double" of human activities. Bangkun will implement "on the in-depth development of the grassroots, the people to worry, notification on the normalization of promoting harmony activities, making the double work innovation system, the demands of the masses reflect back channels, help enterprises to solve practical problems as much as possible. 3, visit OK~ 注:由于eclipse经常有一些Bug,所以,如果修改的代码总是不生效,可以考虑下面两种方法,“严酷”程序由低到高: (一) 停止程序并清除编译文件 停止程序: 清除编译文件: (二) 最狠的莫过于重启eclipse,不过,这仍不失为一个相当有效而又没法解释的好办法。 》点击查看原文... to correcting misunderstandings advocate good cadre style. 2, to carry out the "double" of human activities. Bangkun will implement "on the in-depth development of the grassroots, the people to worry, notification on the normalization of promoting harmony activities, making the double work innovation system, the demands of the masses reflect back channels, help enterprises to solve practical problems as much as possible. 3, visit 相信很多人发现在论坛里突然不能上传图片了,当选择上传图片时,会提示“无效的图片文 件”,起初我本人也以为是先前论坛改版的缘故,但后来仔细回想以后,发觉不能上传图片 是从IE浏览器升级到IE8后开始的,在网上搜索了一下,IE8浏览器果然存在升级后无法 上传图片等附件的问题,而解决的办法也很简单,如图,在“Internet选项”的“安全”项里选择“自定义级别”,把“其他”项下面的“将文件上载到服务器时包含本地目录路 径”改为“启用”以后,保存重启浏览器,图片等附件就可以正常上传了。 to correcting misunderstandings advocate good cadre style. 2, to carry out the "double" of human activities. Bangkun will implement "on the in-depth development of the grassroots, the people to worry, notification on the normalization of promoting harmony activities, making the double work innovation system, the demands of the masses reflect back channels, help enterprises to solve practical problems as much as possible. 3, visit
/
本文档为【jsp上传图片方法】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索