java 求jsp上传图片到服务器代码

java 求jsp上传图片到服务器代码,第1张

提交页面表单
<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());
%>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/10273695.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-07
下一篇 2023-05-07

发表评论

登录后才能评论

评论列表(0条)

保存