java图片上传源代码 java上传图片到tomcat

java 求jsp上传图片到服务器代码提交页面表单
form action="up.jsp" enctype="multipart/form-data" method="post"
input type="file" name="file"
input type="submit" value="https://www.04ip.com/post/确定"
/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());
%
解释一下这段JAVA 关于图片上传的代码private File file;
private String fileFileName;
private String picture;
//都有getter 和 setter
InputStream is = new FileInputStream(file);
//引入一个IO流java图片上传源代码的输入流
String root = ServletActionContext.getRequest()
.getRealPath("/bookpicture");
//通过REQUEST来得到相对地址java图片上传源代码 , 并在后面加上/bookpicture
File f = new File(root, this.getFileFileName());
//定义一个FILE文件,第一个参数是文件的路径,第二个是文件的名字
picture="."+"\\"+"bookpicture"+"\\"+this.getFileFileName();
//为PICTURE字符串赋值,/地址/文件名
System.out.println
("======picture====="+picture);
//从控制台输出Picture
OutputStream os = new FileOutputStream(f);
//第一个文件的输出流
byte[] buffer = new byte[1024];
//定义一个bufer的字符串,长度为1024
int len = 0;
while ((len = is.read(buffer))0) {
//如果从制定文件中读取到的信息为结束就继续循环
os.write(buffer, 0, len);
//将文件读出的内容写入到指定的文件中
}
求JAVA上传图片代码public String imagesUpload(){
log.debug("images upload");
if(files == null){
saveMessage("没有上传任何文件!");
return ERROR;
}
//查看上传临时目录是否存在
String sep = File.separator;
String condPath = sep + "upload" + sep +"tmp" +sep;
String uploadDir = ServletActionContext.getServletContext().getRealPath(condPath) + sep;
File dirPath = new File(uploadDir);
if (!dirPath.exists())
dirPath.mkdirs();
String extension, fileName;
int count = 0;
StringBuilder picNameSB = new StringBuilder("");
ListString picNamesSet = new ArrayListString();
try{
for(File tempFile : files){
if(count0)
picNameSB.append("##");

推荐阅读