jsp图片上传代码(jsp上传图片到文件夹)
admin 发布:2022-12-19 20:02 136
今天给各位分享jsp图片上传代码的知识,其中也会对jsp上传图片到文件夹进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、通过JSP怎样上传图片到服务器
- 2、谁有jsp上传图片的代码了,把上传的图片保存到文件夹 里的,简单点的,谢谢啊,急,
- 3、在jsp中做照片上传预览的代码
- 4、JSP如何上传图片?
- 5、java 求jsp上传图片到服务器代码
通过JSP怎样上传图片到服务器
1.限制文件上传类型只能是图片
function checkFileType(name,file){
var extArray = new Array(".doc",".docx");
var allowSubmit = false;
if (!file){
return;
}
while (file.indexOf("\\") != -1){
file = file.slice(file.indexOf("\\") + 1);
}
var ext = file.slice(file.indexOf(".")).toLowerCase();
for (var i = 0; i extArray.length; i++) {
if (extArray[i] == ext){
allowSubmit = true;
break;
}
}
if(!allowSubmit){
alert("只能上传以下格式的文件:"+ (extArray.join("")) + "\n请重新选择再上传.");
document.getElementById(name).value = "";
}
}
其中:extArray是要求文件类型。可自行定义。
2.引入jQuery外部文件
jquery-2.1.4.min.js
3.编写js代码
$(function () {
$('#txtfilePath1').uploadReview({
width: 350,
height: 350,
target: '#uploadReview1_content'
});
});
其中:txtfilePath1是input:file。width,height是预览图片的宽度和高度。target是显示预览图片的位置。
4.编写jsp页面代码
body
input type="text" class="yourClassName" name="filePath1" id="filePath1"/
input type="file" id="txtfilePath1" name="txtfilePath1" style="display:none;"
input type="button" onclick="txtfilePath1.click()" id="fileup1" name="fileup1" class="searchThing"value="上传"
/body
注: 这个是很久以前在网上看到的,就整理了下来,但是这么久都没用过,所以也没调试过,你自己试一试研究研究, 再来网上很多博客里,他们写的很详细的,可以去看看
谁有jsp上传图片的代码了,把上传的图片保存到文件夹 里的,简单点的,谢谢啊,急,
这个比较简单
选择图片的jsp页面的form
form action="doUploadImage.jsp" encType=multipart/form-data method=post
本地选择:
input type="file" name="selPicture"
style="width: 330px; height: 23px; font-size: 16px"
input type="submit" name="upload" id="upload" value="上传"
style="width: 70px; height: 25px"
/form
接收页面
%@ page language="java" import="java.util.*,com.jspsmart.upload.*,java.io.*"
pageEncoding="GBK"%
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
html
head
titleMy JSP 'doUploadImage.jsp' starting page/title
/head
body
%
request.setCharacterEncoding("GBK");
long size = 5 * 1024 * 1024;//允许上传最大值为5MB
String fileType = "jpg,gif,JPG";//允许上传文件类型
String imgName = null;//图片名称
byte[] data = null;//数据
String filePath = "";//文件路径
//得到服务器目录webroot下的ImageFiles目录的完整路径
String path = super.getServletContext().getRealPath("/Image");
System.out.println(path);
SmartUpload su = new SmartUpload();
//初始化
su.initialize(pageContext);
su.setMaxFileSize(size);
su.setAllowedFilesList(fileType);
su.setCharSet("GBK");
//上载文件
su.upload();
System.out.println(su.getSize());
su.getRequest();
//循环取得所有上载的文件
Files files = su.getFiles();
if (files != null) {
//如果文件路径不存在则生成路径
java.io.File fileDir = new java.io.File(path);
System.out.println("存在");
if (!fileDir.exists()) {
fileDir.mkdirs();
System.out.println("不存在");
}
System.out.println(files.getCount());
//取出文件
for (int i = 0; i files.getCount(); i++)
{
com.jspsmart.upload.File file = files.getFile(i);
if (file.isMissing()) continue;
if ("selPicture".equals(file.getFieldName())) {
String type = file.getFilePathName();
type = type.substring(type.lastIndexOf("."));
imgName = UUID.randomUUID().toString();//生成uuid作为图片的名称
imgName += type;
filePath = path + "/" + imgName;
//保存到指定文件
file.saveAs(filePath);
//读取文件
data = readFile(filePath);
break;
}
}
}
if (data == null) {
out.print("没有图片");
} else {
out.print("图片上传成功");
}
%
%!byte[] readFile(String filePath) {
ByteArrayOutputStream bos = null;
try {
FileInputStream fs = new FileInputStream(filePath);
bos = new ByteArrayOutputStream(5 * 1024 * 1024);
byte[] b = new byte[1024];
int len;
while ((len = fs.read(b)) != -1) {
bos.write(b, 0, len);
}
fs.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (bos == null) {
return null;
} else {
return bos.toByteArray();
}
}
%
%=request.getParameter("name") %
/body
/html
有问题q我 379726806
后面data那一段时测试的 用的时候删除掉 这是我写的一个测试小工程 在项目里面用的时候是把接收图片放在servlet中的
我也是才搞了一个图片上传的东东
在jsp中做照片上传预览的代码
建议你使用 Web upload 组件,可图片预览, 可多选,可拖放上传,可粘贴后直接上传
百度开源产品.
俩三行代码就可以直接使用
具体网址 :
JSP如何上传图片?
如果你是纯JSP写的,可以用SmartUpload.在你的页面form 里 form action="doUpload.jsp" method="POST" enctype="multipart/form-data"
文件名:input type="text" name="name"/br
请选择上传的文件:input type="file" name="file1"/
input type="submit" value="上传"/
/form 注意:enctype="multipart/form-data"这个一定要这样设置,具体什么意思我也不是很清楚.....(呵呵) 提交到执行的页面如下: //实例化上传组件
SmartUpload upload = new SmartUpload();
//初始化上传组件
upload.initialize(this.getServletConfig(), request, response);
//开始上传
upload.upload();
//获取上传的文件列表对象
Files f = upload.getFiles();
//获取文件对象
File fil = f.getFile(0);
//去掉文件后缀
String ext = fil.getFileExt();
//判断文件类型是否是jpg格式jpg,gif,bmp,png,JPG,GIF,BMP,PNG
if (!(ext.equals("jpg")) !(ext.equals("gif")) !(ext.equals("bmp")) !(ext.equals("png")) !(ext.equals("JPG")) !(ext.equals("GIF")) !(ext.equals("BMP")) !(ext.equals("PNG"))) {
out.println("script type='text/javascript'alert('文件类型错误');location.replace('upLoadPhoto.jsp');/script");
return;
}
//满足条件进行文件的上传uploadImages在webRoot文件夹下的一个目录
fil.saveAs("uploadImages/" + fil.getFileName());
String filepath = "uploadImages/" + fil.getFileName(); //保存到数据库的路径 OK.这样就可以了.....
java 求jsp上传图片到服务器代码
提交页面表单
form action="up.jsp" enctype="multipart/form-data" method="post"
input type="file" name="file"
input type="submit" value="确定"
/form
上传页面up.jsp
%@page import="java.io.FileWriter"%
%@ page language="java" contentType="text/html; charset=UTF-8"
import="java.io.*"
pageEncoding="UTF-8"%
%
/**
协议头四行内容
45 -----------------------------7de231211204c4
80 Content-Disposition: form-data; name="file"; filename="xx.txt"
26 Content-Type: text/plain
2
标记文件结尾
-----------------------------7de231211204c4--
**/
ServletInputStream sin = request.getInputStream();
byte[] buffer = new byte[1024 * 8];
int length = 0, row = 0;
String mark = "";
String filename = "";
while ((length = sin.readLine(buffer, 0, buffer.length)) 0) {
out.println(length + " " + new String(buffer, 0, length, "UTF-8") + "br");
String s = new String(buffer, 0, length, "UTF-8");
if (row == 0)
mark = s.trim();
else if (s.indexOf("filename=") 0) {
int end = s.lastIndexOf("\"");
int start = s.substring(0, end).lastIndexOf("\"");
filename = s.substring(start + 1, end);
} else if ("".equals(s.trim()))
break;
row ++;
}
out.println("filename: " + filename + "br");
filename = request.getRealPath("/") + "../" + filename;
FileOutputStream fout = new FileOutputStream(filename);
while ((length = sin.readLine(buffer, 0, buffer.length)) 0) {
String s = new String(buffer, 0, length);
if (s.startsWith(mark))
break;
fout.write(buffer, 0, length);
}
fout.flush();
fout.close();
File f = new File(filename);
out.println(f.exists());
out.println(f.getAbsolutePath());
%
jsp图片上传代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于jsp上传图片到文件夹、jsp图片上传代码的信息别忘了在本站进行查找喔。
版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;
相关推荐
- 04-20带下拉的左侧页面代码(下拉式导航栏代码)[20240420更新]
- 04-20php仿代码工具运行送吗(vscode怎么运行PHP代码)[20240420更新]
- 04-20日历控件代码(js日历控件)[20240420更新]
- 04-20复制框代码(复制的代码)[20240420更新]
- 04-20shop开源代码(shop商城开源免费版)[20240420更新]
- 04-20flash刮风代码(flash绘制风车的过程)[20240420更新]
- 04-20h5弹窗代码(h5文本框代码)[20240420更新]
- 04-20易语言代码编辑框教程(易语言代码示例)[20240420更新]
- 04-20jquery购物车代码(jquery添加商品到购物车)[20240420更新]
- 04-20天猫两侧代码(天猫国际代码)[20240420更新]
取消回复欢迎 你 发表评论:
- 标签列表
- 最近发表
- 友情链接