java网络上传图片代码 java中图片文件上传怎么实现

解释一下这段JAVA 关于图片上传的代码private File file;
private String fileFileName;
private String picture;
//都有getter 和 setter
InputStream is = new FileInputStream(file);
//引入一个IO流的输入流
String root = ServletActionContext.getRequest()
.getRealPath("/bookpicture");
//通过REQUEST来得到相对地址,并在后面加上/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("##");
extension = UserUtil.getFileExtension(filesFileName[count]);
fileName = generatePictureName(uploadDir, doType + count, extension);
File newFile = new File(fileName);
UserUtil.copyFileContent(tempFile,newFile);
picNamesSet.add(newFile.getName());
picNameSB.append(newFile.getName());
count++ ;
}
getSession().setAttribute("picNamesSet",picNamesSet);
picName = picNameSB.toString();
}catch(IOException e){
e.printStackTrace();
return ERROR;
}
int x= 1;
return SUCCESS;
}
private String generatePictureName(final String dir, final String suffix, final String fileExtension){
StringBuffer name = new StringBuffer(dir);
StringtmpFileName = String.valueOf(System.currentTimeMillis());
【java网络上传图片代码 java中图片文件上传怎么实现】if(suffix != null)
tmpFileName += suffix;
tmpFileName += "." + fileExtension;
name.append(tmpFileName);
return name.toString();
}
java 中如何向服务器上传图片我们使用一些已有的组件帮助我们实现这种上传功能 。
常用的上传组件:
Apache 的 Commons FileUpload
JavaZoom的UploadBean
jspSmartUpload
以下,以FileUpload为例讲解
1、在jsp端
form id="form1" name="form1" method="post" action="servlet/fileServlet" enctype="multipart/form-data"
要注意enctype="multipart/form-data"
然后只需要放置一个file控件,并执行submit操作即可
input name="file" type="file" size="20"
input type="submit" name="submit" value="https://www.04ip.com/post/提交"
2、web端
核心代码如下:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);

推荐阅读