急!java 上传图片代码!
--------------------编程问答--------------------你看看的行不
private String imagesFileName ;--------------------编程问答-------------------- 我可以把这段代码放到service中吗 --------------------编程问答--------------------
private File images ;
public String addGatewayInfor(){
String[] str = { ".jpg", ".jpeg", ".bmp", ".gif" ,".png" };
if (imagesFileName != null ) {
for(String s : str){
if(imagesFileName.toLowerCase().endsWith(s)){
try {
FileUtils.copyFile(images, new File("你的路径" + Calendar.getInstance().getTimeInMillis()+s));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return "" ;
}
MultipartHttpServletRequest multipartRequest = null;
try {
multipartRequest = (MultipartHttpServletRequest) request;
} catch (Exception e) {
// TODO: handle exception
}
if(multipartRequest!=null){
//用户身份证号码扫描件
MultipartFile imgFile1= multipartRequest.getFile("userIdentityCardScan_fleet");
if(imgFile1!=null){
String filename=imgFile1.getOriginalFilename();
if(imgFile1.getSize()>0){
try {
SaveFileFromInputStream(imgFile1.getInputStream(),"D:/",filename);
} catch (IOException e) {
System.out.println(e.getMessage());
}
}else{
throw new Exception("上传失败:上传文件不能为?空");
}
}
}
public void SaveFileFromInputStream(InputStream stream,String path,String filename) throws IOException {
FileOutputStream fs=new FileOutputStream( path + "/"+ filename);
byte[] buffer =new byte[1024*1024];
int bytesum = 0;
int byteread = 0;
while ((byteread=stream.read(buffer))!=-1){
bytesum+=byteread;
fs.write(buffer,0,byteread);
fs.flush();
}
fs.close();
stream.close();
}
我这里是有spring mvc上传的,你看代码改一下应该能用的。上面是在@Controller的代码
<input id="userIdentityCardScan_fleet" name="userIdentityCardScan_fleet" type="file" size="28" />
然后是根据文件名读取图片显示到页面显示出来,因为我这边图片是保存到数据库的,跟你这个存路径有点区别,你可以改改
[code=HTML]
<img id="preImg2" src="review_viewimage.jspx?groupType=${(cmsMember.group.name)!}&id=${(cmsMember.id)!}&fieldName=userIdentityCardScan" style="width:100px;height:70px;background-color:#CCCCCC;border:1px solid #333"/>
后面传的参数自己改改,可以传id啊什么的,然后后台通过id获取当保存的路径
@RequestMapping("/member/review_viewimage.jspx")
public String allianceViewImages(Integer id,String groupType,String fieldName,
HttpServletRequest request,
HttpServletResponse response, ModelMap model) {
Blob blob = getImageBlob(groupType, fieldName, id);
if(blob!=null){
InputStream input = null;
try {
input = blob.getBinaryStream();//我这里直接从数据库得到流
//input = new FileInputStream(new File(imageFile)); //imageFile这里是你保存的文件路径
response.setContentType("image/*"); // 设置返回的文件类型
OutputStream outStream = response.getOutputStream(); // 得到向客户端输出二进制数据的对象
byte[] buffer=new byte[32768];
int n=0;
while((n=input.read(buffer))!=-1){
outStream.write(buffer,0,n);
}
input.close();
} catch (SQLException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
} catch (IOException e) {
// 错误处理
PrintWriter toClient;
try {
toClient = response.getWriter(); // 得到向客户端输出文本的对象
response.setContentType("text/html;charset=utf-8");
toClient.write("无法打开图片!");
toClient.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}catch (NullPointerException n){
// 错误处理
PrintWriter toClient;
try {
toClient = response.getWriter(); // 得到向客户端输出文本的对象
response.setContentType("text/html;charset=utf-8");
toClient.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
return null;
}
[/code]
补充:Java , Java相关