java 求jsp上传图片到服务器代码
4个回答
展开全部
提交页面表单
<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());
%>
<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());
%>
展开全部
<form id="addform" method="post" enctype="multipart/form-data" action="${base}/action/addPhoPic">
<table border="0" class="perview" align="center">
<a href="#" onClick="toaddpic()">上传至相册</a>
<tr>
<th>选择文件</th>
<th width="50%">预览图</th>
</tr>
<tr>
<td height="200"><input id="idFile" name="upload" type="file" /></td>
<td align="center"><img id="idImg" /></td>
</tr>
</table>
</form>
Java后台处理:
//与前台页面空间name一致
private File upload;
//反射,得到文件类型,文件名称
private String uploadContentType;
private String uploadFileName;
public String doAddPhoPic(){
//自己的PhotoService接口处理
IPhotoService photoService=BeanFactory.getBean(BeanConstants.WB_PHOTO_SERVICE);
Photo photo=new Photo();
//这里简单的demo没有要把名字规范,也没有对图片有剪切或缩小处理,所以就简单的把上传的图片以1,2,3命名
int count=photoService.queryPhotoList().size();
count++;
String file_path1="";
String file_path2="";
try {
//上传至该项目所在本地目录
file_path1=Constants.BASE_ROOT+"/fullsize"+"/"+count+".jpg";
file_path2=Constants.BASE_ROOT+"/thumbs"+"/"+count+".jpg";
photo.setPicName(photoService.queryPhotoList().size()+1+".jpg");
photo.setPicUrl(file_path2);
photoService.insertPhoto(photo);
System.out.println("---"+file_path1);
System.out.println("---"+file_path2);
//对文件进行写操作
FileOutputStream fos1=new FileOutputStream(file_path1);
FileOutputStream fos2=new FileOutputStream(file_path2);
//对文件进行读操作
FileInputStream fis=new FileInputStream(upload);
byte[] buffer=new byte[1024];
int len=0;
//读入流,保存至byte数组
while((len=fis.read(buffer))>0){
fos1.write(buffer,0,len);
fos2.write(buffer,0,len);
}
fos1.close();
fos2.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
list=photoService.queryPhotoList();
return SUCCESS;
}
<table border="0" class="perview" align="center">
<a href="#" onClick="toaddpic()">上传至相册</a>
<tr>
<th>选择文件</th>
<th width="50%">预览图</th>
</tr>
<tr>
<td height="200"><input id="idFile" name="upload" type="file" /></td>
<td align="center"><img id="idImg" /></td>
</tr>
</table>
</form>
Java后台处理:
//与前台页面空间name一致
private File upload;
//反射,得到文件类型,文件名称
private String uploadContentType;
private String uploadFileName;
public String doAddPhoPic(){
//自己的PhotoService接口处理
IPhotoService photoService=BeanFactory.getBean(BeanConstants.WB_PHOTO_SERVICE);
Photo photo=new Photo();
//这里简单的demo没有要把名字规范,也没有对图片有剪切或缩小处理,所以就简单的把上传的图片以1,2,3命名
int count=photoService.queryPhotoList().size();
count++;
String file_path1="";
String file_path2="";
try {
//上传至该项目所在本地目录
file_path1=Constants.BASE_ROOT+"/fullsize"+"/"+count+".jpg";
file_path2=Constants.BASE_ROOT+"/thumbs"+"/"+count+".jpg";
photo.setPicName(photoService.queryPhotoList().size()+1+".jpg");
photo.setPicUrl(file_path2);
photoService.insertPhoto(photo);
System.out.println("---"+file_path1);
System.out.println("---"+file_path2);
//对文件进行写操作
FileOutputStream fos1=new FileOutputStream(file_path1);
FileOutputStream fos2=new FileOutputStream(file_path2);
//对文件进行读操作
FileInputStream fis=new FileInputStream(upload);
byte[] buffer=new byte[1024];
int len=0;
//读入流,保存至byte数组
while((len=fis.read(buffer))>0){
fos1.write(buffer,0,len);
fos2.write(buffer,0,len);
}
fos1.close();
fos2.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
list=photoService.queryPhotoList();
return SUCCESS;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我有struts写的
更多追问追答
追问
能上传上来我看看吗 ?
追答
tomcat服务器么? 数据库是什么的?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |