
提交页面表单
<form action="upjsp" enctype="multipart/form-data" method="post">
<input type="file" name="file">
<input type="submit" value="确定">
</form>
上传页面upjsp
<%@page import="javaioFileWriter"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
import="javaio"
pageEncoding="UTF-8"%>
<%
/
协议头四行内容
45 -----------------------------7de231211204c4
80 Content-Disposition: form-data; name="file"; filename="xxtxt"
26 Content-Type: text/plain
2
标记文件结尾
-----------------------------7de231211204c4--
/
ServletInputStream sin = requestgetInputStream();
byte[] buffer = new byte[1024 8];
int length = 0, row = 0;
String mark = "";
String filename = "";
while ((length = sinreadLine(buffer, 0, bufferlength)) > 0) {
outprintln(length + " " + new String(buffer, 0, length, "UTF-8") + "<br>");
String s = new String(buffer, 0, length, "UTF-8");
if (row == 0)
mark = strim();
else if (sindexOf("filename=") > 0) {
int end = slastIndexOf("\"");
int start = ssubstring(0, end)lastIndexOf("\"");
filename = ssubstring(start + 1, end);
} else if (""equals(strim()))
break;
row ++;
}
outprintln("filename: " + filename + "<br>");
filename = requestgetRealPath("/") + "/" + filename;
FileOutputStream fout = new FileOutputStream(filename);
while ((length = sinreadLine(buffer, 0, bufferlength)) > 0) {
String s = new String(buffer, 0, length);
if (sstartsWith(mark))
break;
foutwrite(buffer, 0, length);
}
foutflush();
foutclose();
File f = new File(filename);
outprintln(fexists());
outprintln(fgetAbsolutePath());
%>
这个比较简单
选择的jsp页面的form
<form action="doUploadImagejsp" 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="javautil,comjspsmartupload,javaio"
pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 401 Transitional//EN">
<html>
<head>
<title>My JSP 'doUploadImagejsp' starting page</title>
</head>
<body>
<%
requestsetCharacterEncoding("GBK");
long size = 5 1024 1024;//允许上传最大值为5MB
String fileType = "jpg,gif,JPG";//允许上传文件类型
String imgName = null;//名称
byte[] data = null;//数据
String filePath = "";//文件路径
//得到服务器目录webroot下的ImageFiles目录的完整路径
String path = supergetServletContext()getRealPath("/Image");
Systemoutprintln(path);
SmartUpload su = new SmartUpload();
//初始化
suinitialize(pageContext);
susetMaxFileSize(size);
susetAllowedFilesList(fileType);
susetCharSet("GBK");
//上载文件
suupload();
Systemoutprintln(sugetSize());
sugetRequest();
//循环取得所有上载的文件
Files files = sugetFiles();
if (files != null) {
//如果文件路径不存在则生成路径
javaioFile fileDir = new javaioFile(path);
Systemoutprintln("存在");
if (!fileDirexists()) {
fileDirmkdirs();
Systemoutprintln("不存在");
}
Systemoutprintln(filesgetCount());
//取出文件
for (int i = 0; i < filesgetCount(); i++)
{
comjspsmartuploadFile file = filesgetFile(i);
if (fileisMissing()) continue;
if ("selPicture"equals(filegetFieldName())) {
String type = filegetFilePathName();
type = typesubstring(typelastIndexOf(""));
imgName = UUIDrandomUUID()toString();//生成uuid作为的名称
imgName += type;
filePath = path + "/" + imgName;
//保存到指定文件
filesaveAs(filePath);
//读取文件
data = readFile(filePath);
break;
}
}
}
if (data == null) {
outprint("没有");
} else {
outprint("上传成功");
}
%>
<%!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 = fsread(b)) != -1) {
boswrite(b, 0, len);
}
fsclose();
} catch (FileNotFoundException e) {
eprintStackTrace();
} catch (IOException e) {
eprintStackTrace();
}
if (bos == null) {
return null;
} else {
return bostoByteArray();
}
}
%>
<%=requestgetParameter("name") %>
</body>
</html>
有问题q我 379726806
后面data那一段时测试的 用的时候删除掉 这是我写的一个测试小工程 在项目里面用的时候是把接收放在servlet中的
我也是才搞了一个上传的东东
这个是我以前用的一个用jsp上传的,servlet是一样的,你可以自己修改下。
普通参数也是可以接受的
在jsp页面中你需要导入jsmart的中文jar包,如果你需要的话可以密我,我邮箱发给你
上传页面uploadhtml
<html>
<head>
<title></title>
<meta >
<html xmlns=">
用第三方工具去取 common-upload,具体取到的方法参考代码如下:
FileItemFactory fileItemFactory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(fileItemFactory);
uploadsetHeaderEncoding("utf-8");
try {
List<FileItem> items = uploadparseRequest(request);
for (FileItem fileItem : items) {
Systemoutprintln("fileName=" + fileItemgetFieldName());
//获取文件流
InputStream in = fileItemgetInputStream();
ServletContext context = getServletConfig()getServletContext();
String path = contextgetRealPath("image");
Systemoutprintln(path);
OutputStream out = new FileOutputStream(new File(path + "\\" + fileItemgetName()));
byte[] buffer = new byte[1024];
int len = 0;
while((len = inread(buffer)) != -1) {
outwrite(buffer, 0, len);
}
outclose();
inclose();
Systemoutprintln("写入完毕");
}
} catch (FileUploadException e) {
eprintStackTrace();
}
以上就是关于java 求jsp上传图片到服务器代码全部的内容,包括:java 求jsp上传图片到服务器代码、谁有jsp上传图片的代码了,把上传的图片保存到文件夹 里的,简单点的,谢谢啊,急,、请问jsp页面如何上传照片到服务器呢,等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)