如何用jsp把客户端的文件上传到服务器什么去????把文件保存在服务器什么的一个指定目录下面
3个回答
展开全部
String path = getServletContext().getRealPath("/");
try {
BufferedInputStream bis = new BufferedInputStream(request.getInputStream());
//创建临时文件
RandomAccessFile raf = new RandomAccessFile(path + "image//temp.jpg", "rw");
byte b[] = new byte[1000];
int n;
while ((n = bis.read(b)) != -1) {
raf.write(b, 0, n);
}
bis.close();
raf.seek(0);
for (int i = 0; i < 4; i++) {
raf.readLine();
}
long begin = raf.getFilePointer();
long m = raf.length() - 1;
raf.seek(m);
for (int i = 0; m >= 0 && (n = raf.readByte()) != -1 && i < 2;) {
if (n == '\n') {
i++;
}
raf.seek(m = raf.getFilePointer() - 2);
}
long end = raf.getFilePointer();
RandomAccessFile raf2 = new RandomAccessFile(path + "image//" + id + ".jpg", "rw");
raf.seek(begin);
for (long i = begin; i < end; i++) {
raf2.write(raf.readByte());
}
raf.close();
raf2.close();
//删除临时文件
File temp=new File(path+"image//temp.jpg");
temp.delete();
session.removeAttribute("photo");
out.print("<script>alert('更新成功');");//仍需刷新
out.print("window.location='Admin_view_book.jsp?id=" + id + "';</script>");
} catch (Exception e) {
e.printStackTrace();
out.print("<script>alert('出现错误');history.back();</script>");
}
try {
BufferedInputStream bis = new BufferedInputStream(request.getInputStream());
//创建临时文件
RandomAccessFile raf = new RandomAccessFile(path + "image//temp.jpg", "rw");
byte b[] = new byte[1000];
int n;
while ((n = bis.read(b)) != -1) {
raf.write(b, 0, n);
}
bis.close();
raf.seek(0);
for (int i = 0; i < 4; i++) {
raf.readLine();
}
long begin = raf.getFilePointer();
long m = raf.length() - 1;
raf.seek(m);
for (int i = 0; m >= 0 && (n = raf.readByte()) != -1 && i < 2;) {
if (n == '\n') {
i++;
}
raf.seek(m = raf.getFilePointer() - 2);
}
long end = raf.getFilePointer();
RandomAccessFile raf2 = new RandomAccessFile(path + "image//" + id + ".jpg", "rw");
raf.seek(begin);
for (long i = begin; i < end; i++) {
raf2.write(raf.readByte());
}
raf.close();
raf2.close();
//删除临时文件
File temp=new File(path+"image//temp.jpg");
temp.delete();
session.removeAttribute("photo");
out.print("<script>alert('更新成功');");//仍需刷新
out.print("window.location='Admin_view_book.jsp?id=" + id + "';</script>");
} catch (Exception e) {
e.printStackTrace();
out.print("<script>alert('出现错误');history.back();</script>");
}
展开全部
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用session比较常见!
jsp端的代码写到<%%>中!
jsp端的代码写到<%%>中!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询