重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
首先希望我的回答能给你带来帮助
十年的盘龙网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。营销型网站建设的优势是能够根据用户设备显示端的尺寸不同,自动调整盘龙建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联建站从事“盘龙网站设计”,“盘龙网站推广”以来,每个客户项目都认真落实执行。
首先写一个上传类
public class products_shang_chuang {
public static boolean GOTO(String paht,FormFile file)
{
boolean bool =false;
int betered = 0;
byte[] buufer = new byte [2042];
if(file.getFileSize()0file.getFileSize()500000)
{
try {
InputStream in = file.getInputStream();//写入文件
OutputStream out = new FileOutputStream(paht);
while((betered=in.read(buufer, 0, 2042))!=-1)
{
out.write(buufer, 0, betered);//将文件写入服务器
}
in.close();
out.close();
bool = true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
bool = false;
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
bool = false;
e.printStackTrace();
}
}
return bool;
}
public static boolean delete_file(String path)
{
File file = new File(path);
boolean bool = false;
if(file.isFile())
{
System.out.println("文件存在");
bool = file.delete();
}
return bool;
}
}
这个类主要是上传和删除图片的类
那么下来直接调用就可以了 通过页面往action中提交数据
if(form instanceof Products_addForm)//如果form是FilesForm
{
String engding = request.getCharacterEncoding();
if((engding != null) (engding.equalsIgnoreCase("utf-8")))
{
response.setContentType("text/html; charset=gb2312");//如果没有指定编码,编码格式为gb2312
}
FormFile file = products_addForm.getFiles();
inpa = Huoqu_weiyi_biaoshi.getUniqueId();
String path = request.getRealPath("/guanliyuan/ji_zhu_products_img/"+(inpa+file.getFileName()));
System.out.println(path);
String name = file.getFileName();
inpa = "/guanliyuan/ji_zhu_products_img/"+inpa+file.getFileName();
bool = products_shang_chuang.GOTO(path, file);
}
这样就把图片上传上去了 仔细看看 批量上传和这个一样只是需要做一点点的改动而已
有什么不明白的可以联系我Q号:549726411
提交页面表单
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());
%
使用一些已有的组件帮助我们实现这种上传功能。常用的上传组件:Apache的CommonsFileUploadJavaZoom的UploadBeanjspSmartUpload以下,以FileUpload为例讲解1、在jsp端要注意enctype="multipart/form-data"然后只需要放置一个file控件,并执行submit操作即可2、web端核心代码如下:publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{request.setCharacterEncoding("UTF-8");DiskFileItemFactoryfactory=newDiskFileItemFactory();ServletFileUploadupload=newServletFileUpload(factory);try{Listitems=upload.parseRequest(request);Iteratoritr=items.iterator();while(itr.hasNext()){FileItemitem=(FileItem)itr.next();if(item.isFormField()){System.out.println("表单参数名:"+item.getFieldName()+",表单参数值:"+item.getString("UTF-8"));}else{if(item.getName()!=null!item.getName().equals("")){System.out.println("上传文件的大小:"+item.getSize());System.out.println("上传文件的类型:"+item.getContentType());System.out.println("上传文件的名称:"+item.getName());FiletempFile=newFile(item.getName());Filefile=newFile(sc.getRealPath("/")+savePath,tempFile.getName());item.write(file);request.setAttribute("upload.message","上传文件成功!");}else{request.setAttribute("upload.message","没有选择上传文件!");}}}}catch(FileUploadExceptione){e.printStackTrace();}catch(Exceptione){e.printStackTrace();request.setAttribute("upload.message","上传文件失败!");}request.getRequestDispatcher("/uploadResult.jsp").forward(request,response);}